1、Go的安装
Go最新版安装包的下载(Go官网):https://studygolang.com/dl
WindowsMacOSnextLinuxLinuxgo/usr/local
go version
2、Go环境变量的配置
安装成功后,必须要配置环境变量。
(1) windows环境变量的配置:
windowsgogopathPathgo
GOROOTGo
GOROOT
GOPATHgo
GoGo_Workssrcpkgbin
GOPATHGoGo_Works
(2) 打开GoMOD,配置代理
在这里感谢「七牛云」为我们中国区的Golang开发者提供的代理服务
打开cmd命令窗,分别运行:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
vscodevscode
(3) 在VSCode中安装Go插件
Extensionsgo$GOPATH/srchello/hello.govscode
$GOPATHD:\Program Files\Go_Works$GOPATHD:\Program Files\Go_Works
$GOPATH/srcD:\Program Files\Go_Works/src
Govscodego
(5) Go Modules的使用
Go modGOPATHGoGOPATHGo modnpm
常用命令为
go mod tidy
通俗来说就是将当前的库源码文件所依赖的包,全部安装并记录下来,多的包就删掉,少了的就自动补上
(6) 实例操作
hellogo mod init
cd $GOPATH/src/hello
go mod init
go.modgo.sum
go.mod
hellogo modgo mod tidy
go help mod
vscodelaunch.jsonsetting.json
launch.jsonDebug
{
"version": "0.2.0",
"configurations": [{
"name": "LaunchGo",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {
// xxx.go脚本的路径为E:/Study_self/go_lesson/src/hello/xxx.go
// 修改"GOPATH": "E:/Study_self/go_lesson",
// 在xxx.go脚本目录下运行go mod init hello
"GOPATH": "D:/Program Files/Go_Works",
"GOROOT": "D:/Program Files/Go"
},
"args": [],
//"showLog": true
}
]
}
setting.jsongolangci-lintvscode
将出现红色异常的地方直接删除或者注释即可。之所以保留给你们,是觉得你们可能有些人也会安装相关插件使用
{
"editor.wordWrap": "on",
// 如果useLanguageServer设为true,那么在编写代码时引入本地没有的package时,会自动下载安装
// 就是有时候会非常卡,保存go的编码文件时偶尔会卡死。这点你们自己取舍吧
"go.useLanguageServer": false,
"editor.minimap.renderCharacters": false,
"editor.minimap.enabled": false,
"terminal.external.osxExec": "iTerm.app",
"go.docsTool": "gogetdoc",
"go.testFlags": ["-v","-count=1"],
"go.buildTags": "",
"go.buildFlags": [],
"go.lintFlags": [],
"go.vetFlags": [],
"go.coverOnSave": false,
"go.useCodeSnippetsOnFunctionSuggest": false,
"go.formatTool": "goreturns",
"go.gocodeAutoBuild": false,
"go.goroot": "D:/Program Files/Go",
"go.gopath": "D:/Program Files/Go_Works",
"go.autocompleteUnimportedPackages": true,
"go.formatOnSave": true,
"window.zoomLevel": 0,
"debug.console.fontSize": 16,
"debug.console.lineHeight": 30,
}
致谢:https://www.jb51.net/article/186294.htm