package main import ( "fmt" ) func main() { fmt.Println("Hello World!") }
- vscode会提示你安装go插件 点击 install all 会安装失败 (被墙了)
安装Go插件,就这一个就可以
- 安装Go语言开发工具包
ctrl+Shift+P
输入框中输入go:install
会自动搜索相关命令,选择Go:Install/Update Tools
- 安装失败解决办法:
- 在GOPATH的
src
目录下创建golang.org/x
目录 - 在
GOPATH/src/golang.org/x
目录下 - 执行
git clone https://github.com/golang/tools.git
tools命令 - 执行
git clone https://github.com/golang/lint.git
命令 - 按下
Ctrl/Command+Shift+P
再次执行Go:Install/Update
Tools 命令,在弹出的窗口全选并点击确定,这一次的安装都会SUCCESSED了
git clone https://github.com/golang/tools.git tools go install github.com/ramya-rao-a/go-outline go install github.com/acroca/go-symbols go install golang.org/x/tools/cmd/guru go install golang.org/x/tools/cmd/gorename go install github.com/josharian/impl go install github.com/rogpeppe/godef go install github.com/sqs/goreturns go install github.com/golang/lint/golint go install github.com/cweill/gotests/gotests go install github.com/ramya-rao-a/go-outline go install github.com/acroca/go-symbols go install golang.org/x/tools/cmd/guru go install golang.org/x/tools/cmd/gorename go install github.com/josharian/impl go install github.com/rogpeppe/godef go install github.com/sqs/goreturns go install github.com/golang/lint/golint go install github.com/cweill/gotests/gotests
- 安装失败解决办法 使用go mod 代理安装
Go 1.13 及以上(推荐)
# Windows cmd 执行 go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.io,direct
# Windows PowerShell 执行 $env:GO111MODULE = "on" $env:GOPROXY = "https://goproxy.cn"
# macOS 或 Linux 执行 export GO111MODULE=on export GOPROXY=https://goproxy.cn # 或者 执行 echo "export GO111MODULE=on" >> ~/.profile echo "export GOPROXY=https://goproxy.cn" >> ~/.profile source ~/.profile
// 手动安装 go get -u -v github.com/mdempsky/gocode go get -u -v github.com/uudashr/gopkgs/v2/cmd/gopkgs go get -u -v github.com/ramya-rao-a/go-outline go get -u -v github.com/acroca/go-symbols go get -u -v golang.org/x/tools/cmd/guru go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/cweill/gotests/... go get -u -v github.com/fatih/gomodifytags go get -u -v github.com/josharian/impl go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct go get -u -v github.com/haya14busa/goplay/cmd/goplay go get -u -v github.com/godoctor/godoctor go get -u -v github.com/go-delve/delve/cmd/dlv go get -u -v github.com/stamblerre/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/sqs/goreturns go get -u -v golang.org/x/lint/golint
- 项目下执行:
go mod init 项目名
生成 go.mod - vscode配置go环境
文件–>首选项–>设置
{ "go.buildOnSave": true, "go.buildFlags": [], "go.buildTags": "", "go.lintOnSave": true, "go.formatOnSave": true, "go.gopath": "E:\\gohmoe", "go.goroot": "C:\\go" }
执行:go run 文件名
或者设置F5 launch.json里添加
{ "version": "0.2.0", "configurations": [ { "name": "LaunchGo", "type": "go", "request": "launch", "mode": "auto", "remotePath": "", "port": 5546, "host": "127.0.0.1", "program": "${fileDirname}", "env": { "GOPATH": "E:/GoCode", "GOROOT": "C:/Program Files/Go" }, "args": [], //"showLog": true } ] }
- 按
F5
Debug执行 - 安装
code runner
插件 执行按钮执 行代码