网上有非常多教程,试了好多不行,所以自己动手搞了个vscode装go的教程,绝对可用。
1.安装go
这里选择安装Go1.11.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.
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.安装gcc
mingw下载地址:https://sourceforge.net/projects/mingw-w64/files/Junk/
解压后将bin目录添加到环境变量path中
mingw包解析:
x86_64表示64位,i686 表示32位
threads 在window下使用可以选择win32,如需跨平台就是用posix
dwarf、sjlj 的异常模型选择,推荐使用dwarf即所谓dw2,这个模型便于调试。不过出来的东西较sjlj的东西>体量稍大些。
4.安装visual studio code
下载地址:https://code.visualstudio.com/
安装中文插件
Chinese (Simplified) Language Pack for Visual Studio Code
打开vscode 按快捷键 ctrl+shift+x 搜索go 安装
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插件很蛋疼,好多插件根本就不能装,所以就用批处理来做这些事情
第一种批处理代码:
@echo off
echo 正在运行
md "%GOPATH%/src/golang.org/x"
cd "%GOPATH%/src/golang.org/x"
echo 下载tools等工具
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint
git clone https://github.com/golang/net
git clone https://github.com/golang/text
git clone https://github.com/golang/crypto
git clone https://github.com/golang/sys
md "%GOPATH%/src/github.com/ramya-rao-a"
cd "%GOPATH%/src/github.com/ramya-rao-a"
git clone https://github.com/ramya-rao-a/go-outline.git
md "%GOPATH%/src/github.com/acroca/"
cd "%GOPATH%/src/github.com/acroca/"
git clone https://github.com/acroca/go-symbols.git
md "%GOPATH%/src/github.com/rogpeppe/"
cd "%GOPATH%/src/github.com/rogpeppe/"
git clone https://github.com/rogpeppe/godef.git
md "%GOPATH%/src/github.com/sqs/"
cd "%GOPATH%/src/github.com/sqs/"
git clone https://github.com/sqs/goreturns.git
md "%GOPATH%/src/github.com/cweill/"
cd "%GOPATH%/src/github.com/cweill/"
git clone https://github.com/cweill/gotests.git
md "%GOPATH%/src/github.com/josharian/"
cd "%GOPATH%/src/github.com/josharian/"
git clone https://github.com/josharian/impl.git
md "%GOPATH%/src/github.com/golang/"
cd "%GOPATH%/src/github.com/golang/"
git clone https://github.com/golang/lint.git
md "%GOPATH%/src/github.com/mdempsky/"
cd "%GOPATH%/src/github.com/mdempsky/"
git clone https://github.com/mdempsky/gocode.git
md "%GOPATH%/src/github.com/uudashr/"
cd "%GOPATH%/src/github.com/uudashr/"
git clone https://github.com/uudashr/gopkgs.git
md "%GOPATH%/src/github.com/stamblerre/"
cd "%GOPATH%/src/github.com/stamblerre/"
git clone https://github.com/stamblerre/gocode.git
rem 64位操作系统用
REM md "%GOPATH%/src/github.com/derekparker/"
REM cd "%GOPATH%/src/github.com/derekparker/"
REM git clone https://github.com/derekparker/delve.git
REM md "%GOPATH%/src/github.com/go-delve/"
REM cd "%GOPATH%/src/github.com/go-delve/"
REM git clone https://github.com/go-delve/delve.git
cd "%GOPATH%"
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 %GOPATH%\\bin\\gocode-gomod.exe github.com/stamblerre/gocode
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
第二种批处理代码:
新建一个批处理文件,运行以下内容:
mkdir %GOPATH%\\src\\golang.org\\x
git clone https://github.com/golang/tools.git %GOPATH%\\src\\golang.org\\x\\tools
go get -v github.com/mdempsky/gocode
go get -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -v github.com/ramya-rao-a/go-outline
go get -v github.com/acroca/go-symbols
go get -v golang.org/x/tools/cmd/guru
go get -v golang.org/x/tools/cmd/gorename
go get -v github.com/derekparker/delve/cmd/dlv
go get -v github.com/stamblerre/gocode
go get -v github.com/rogpeppe/godef
go get -v github.com/ianthehat/godef
go get -v github.com/sqs/goreturns
%go get -v github.com/golang/lint%
git clone https://github.com/golang/lint.git %GOPATH%\\src\\golang.org\\x\\lint
go build -o %GOPATH%\\bin\\gocode.exe github.com/mdempsky/gocode
%go build -o %GOPATH%\\bin\\gopkgs.exe github.com/uudashr/gopkgs/cmd/gopkgs%
%go build -o %GOPATH%\\bin\\go-outline.exe github.com/ramya-rao-a/go-outline%
%go build -o %GOPATH%\\bin\\go-symbols.exe github.com/acroca/go-symbols%
%go build -o %GOPATH%\\bin\\guru.exe golang.org/x/tools/cmd/guru%
%go build -o %GOPATH%\\bin\\gorename.exe golang.org/x/tools/cmd/gorename%
%go build -o %GOPATH%\\bin\\dlv.exe github.com/derekparker/delve/cmd/dlv%
go build -o %GOPATH%\\bin\\gocode-gomod.exe github.com/stamblerre/gocode
go build -o %GOPATH%\\bin\\godef.exe github.com/rogpeppe/godef
go build -o %GOPATH%\\bin\\godef-gomod.exe github.com/ianthehat/godef
%go build -o %GOPATH%\\bin\\goreturns.exe github.com/sqs/goreturns%
go build -o %GOPATH%\\bin\\golint.exe golang.org/x/lint/golint
pause
重要说明:调试插件https://github.com/derekparker/delve 不能生成dlv.exe,编译时候会出错 不支持32位系统
我们在vsocde里面安装Code Runner插件
安装好插件后,打开vscode的配置, 文件->首选项->设置
setting.json vscode的配置文件
{
//"files.encoding": "gb2312",
"files.autoGuessEncoding": true,
"code-runner.executorMap": {
"javascript": "node",
"php": "C:\\php\\php.exe",
"python": "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"
}
}
网友评论