Visual Studio Code搭建Go环境(不定时更新)

网上有非常多教程,试了好多不行,所以自己动手搞了个vscode装go的教程,绝对可用。

1.安装go

  • 这里选择安装 go1.12.5版本
  • 下载地址:https://studygolang.com/dl
  • 安装过程就不想细说了,安装完会自动配置GOROOT环境变量。

GOROOT :Go语言的安装路径。
GOBIN:Go 的二进制文件存放目录(不设置默认成生的位置%GOROOT%\bin)
GOPATH :Go语言的工作空间,不能与 GOROOT 相同。
Path : %GOROOT%\bin\ 和 %GOPATH%\bin\

  • GOPATH 下应有三个目录:

src :包含Go的源文件,它们被组织成包(每个目录都对应一个包)
pkg:包含包对象,也即编译好的库文件 *.a
bin:包含可执行命令

  • 查看go的环境变量使用go env

下面是批处理设置GO的工作目录 setx.exe win7以上系统自带

@ECHO OFF 
IF EXIST %GOROOT%\bin\go.exe ( 
IF EXIST %systemroot%\system32\setx.exe ( 
rem 处理器架构 386/amd64/arm
rem @SETX GOARCH 386 /m 
rem 操作系统 darwin/freebsd/linux/windows
rem @SETX GOOS windows /m

rem 设置工作目录   /m是用户变量,去掉/m是设置成系统变量

echo 设置工作目录
rem @setx GOPATH "%GOPATH%;D:\go" /m
@setx GOPATH "D:\go" /m

echo 设置工作的安装目录 
@setx GOBIN "D:\Go\bin" /m
rem @setx GOBIN "%GOBIN%;D:\Go\bin" /m


echo 启用Go Modules功能,并配置Go的代理
@setx GO111MODULE "on" /m
@setx GOPROXY "https://goproxy.io" /m



echo.
echo.
echo.
echo.
echo.
ECHO    设置成功!
echo.
echo.
echo.
echo.
echo.
) ELSE ( 
@ECHO ON 
@ECHO 未能找到%systemroot%\system32\setx.exe,请下载setx.exe程序 
) 
) ELSE ( 
@ECHO ON 
@ECHO GO安装目录不正确,未能按照预期计划执行
) 
@PAUSE 

2.安装git

  • 下载地址:https://git-scm.com/downloads
  • 下载插件需要用到git工具下载源码

3.安装visual studio code

  • 下载地址:https://code.visualstudio.com/

  • 打开vscode 按快捷键 ctrl+shift+x 搜索以下插件

    • go
    • Chinese (Simplified) Language Pack for Visual Studio Code
    • Code Runner
vscode
  • vscode时需要安装的插件,注意后面的名称
github.com/mdempsky/gocode          gocode
github.com/uudashr/gopkgs/cmd/gopkgs    gopkgs
github.com/ramya-rao-a/go-outline   go-outline
github.com/acroca/go-symbols        go-symbols
golang.org/x/tools/cmd/guru         guru
golang.org/x/tools/cmd/gorename     gorename
github.com/derekparker/delve/cmd/dlv    dlv
github.com/stamblerre/gocode        gocode-gomod
github.com/rogpeppe/godef       godef
github.com/ianthehat/godef          godef-gomod
github.com/sqs/goreturns        goreturns
golang.org/x/lint/golint        golint
  • vscode安装go插件很蛋疼,好多插件根本就不能装,所以就用批处理来做这些事情
    此批处理适合当模块设置成GO111MODULE=on
  • 新建一个文件vscode.bat把以下内容复制进去
@echo off

echo 正在运行




echo 下载tools等工具


git clone https://github.com/golang/tools.git C:/Users/Administrator/go/src/golang.org/x/tools
git clone https://github.com/golang/lint C:/Users/Administrator/go/src/golang.org/x/lint
git clone https://github.com/golang/net C:/Users/Administrator/go/src/golang.org/x/net
git clone https://github.com/golang/text C:/Users/Administrator/go/src/golang.org/x/text
git clone https://github.com/golang/crypto C:/Users/Administrator/go/src/golang.org/x/crypto
git clone https://github.com/golang/sys C:/Users/Administrator/go/src/golang.org/x/sys


git clone https://github.com/ramya-rao-a/go-outline.git C:/Users/Administrator/go/src/github.com/ramya-rao-a/go-outline
git clone https://github.com/acroca/go-symbols.git C:/Users/Administrator/go/src/github.com/acroca/go-symbols
git clone https://github.com/rogpeppe/godef.git C:/Users/Administrator/go/src/github.com/rogpeppe/godef
git clone https://github.com/sqs/goreturns.git C:/Users/Administrator/go/src/github.com/sqs/goreturns
git clone https://github.com/cweill/gotests.git C:/Users/Administrator/go/src/github.com/cweill/gotests
git clone https://github.com/josharian/impl.git C:/Users/Administrator/go/src/github.com/josharian/impl

git clone https://github.com/golang/lint.git C:/Users/Administrator/go/src/github.com/golang/lint
git clone https://github.com/mdempsky/gocode.git C:/Users/Administrator/go/src/github.com/mdempsky/gocode
git clone https://github.com/uudashr/gopkgs.git C:/Users/Administrator/go/src/github.com/uudashr/gopkgs
git clone https://github.com/stamblerre/gocode.git C:/Users/Administrator/go/src/github.com/stamblerre/gocode



rem 64位操作系统用
REM  git clone  https://github.com/derekparker/delve.git  C:/Users/Administrator/go/src/github.com/derekparker/  
REM  git clone https://github.com/go-delve/delve.git C:/Users/Administrator/go/src/github.com/go-delve






cd "C:/Users/Administrator/go"

go install github.com/ramya-rao-a/go-outline
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
go install github.com/acroca/go-symbols
go install github.com/rogpeppe/godef
go install github.com/sqs/goreturns
go install github.com/cweill/gotests/gotests
go install github.com/josharian/impl
go install github.com/golang/lint/golint
go install github.com/mdempsky/gocode
go install github.com/uudashr/gopkgs/cmd/gopkgs
go build -o C:\\Users\\Administrator\\go\\bin\\gocode-gomod.exe github.com/stamblerre/gocode
echo 当模块设置成GO111MODULE=on的时候
go install golang.org/x/tools/cmd/goimports
rem 64位操作系统用
REM go install github.com/derekparker/delve/cmd/dlv
REM go get -v -u  https://github.com/go-delve/delve/delve/cmd/dlv
 @cmd.exe
pause
  • vscode配置
{
  //"files.encoding": "gb2312",
  "python.linting.flake8Enabled": true,
  "python.formatting.provider": "yapf",
  // "files.autoGuessEncoding": true,
  "code-runner.executorMap": {
    "javascript": "node",
    "php": "C:\\php\\php.exe",
    "python": "set PYTHONIOENCODING=utf8 && python",
    "perl": "perl",
    "ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
    "go": "go run",
    "html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
  },
  "python.pythonPath": "C:\\Program Files\\Python37-32\\python.exe",
  "go.autocompleteUnimportedPackages": true,
  "go.formatTool": "goimports", //GO111MODULE=on的时候设置
// "go.formatTool": "goreturns", //在gopath里设置
"go.testFlags":["-v"],
  "editor.formatOnType": true, //保存后自动格式化代码(vscode)
  "editor.formatOnSave": true, //保存后自动格式化代码(vscode)
  

当模块设置成GO111MODULE=auto的时候

  • C:/Users/Administrator/go/src C:/Users/Administrator/go/bin复制到gopath

你可能感兴趣的:(Visual Studio Code搭建Go环境(不定时更新))