本帖最后由 hongsandao 于 2018-6-27 22:54 编辑
0.学习目标1. 环境配置1.1 安装go编译器下载网站地址,改网站地址是国内镜像 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886287729.png?lastModify=1530111258 也可以到官网下载,需要用科学上网 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886112577.png?lastModify=1530111258 我这里下载的是 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886197463.png?lastModify=1530111258 然后进行安装,我这里安装在D file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886429049.png?lastModify=1530111258 1.2 安装 IDE VSCodeSetup-x64-1.24.0.exe file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886638610.png?lastModify=1530111258 1.3 添加GOPATH环境变量1.3.1 手动创建文件夹手动在D盘创建以下目录 GO_MyGo,这个文件名可以是自己起, 根据go的规定,必须创建以下文件夹,文件名不可以更改 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886869833.png?lastModify=1530111258 文件目录结构如下 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528886941311.png?lastModify=1530111258 1.3.2添加GOPATH变量环境GOPATH全部为大写,后面是值是自己创建的文件夹名称 GOROOT为安装go编译器时候自动创建 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528890186356.png?lastModify=1530111258 1.3.3版本验证cmd命名窗口输入 go version file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528887377240.png?lastModify=1530111258 cmd命名窗口输入 go env file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528887421411.png?lastModify=1530111258 cmd 命令输入 go file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528887531908.png?lastModify=1530111258 1.4 安装 Git2.编写测试代码2.1 vs code 安装go扩展插件file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528888424249.png?lastModify=1530111258 创建 D:\GO_MyGo\src\go_dev 目录 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528888595665.png?lastModify=1530111258 2.2 安装go包文件2.2.1 自动安装go包文件添加第一个测试代码 hello.go 文件 vs code 提示安装 go 的包文件,选择全部安装 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528888906380.png?lastModify=1530111258 安装过程如下 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528889063500.png?lastModify=1530111258 由于没有科学上网,有些包没有安装成功 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528891420889.png?lastModify=1530111258 报错信息如下 Installing 10 tools at D:\GO_MyGo\bin gocode gopkgs go-outline go-symbols guru gorename godef goreturns golint dlv Installing github.com/mdempsky/gocode SUCCEEDEDInstalling github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDEDInstalling github.com/ramya-rao-a/go-outline FAILEDInstalling github.com/mdempsky/gocode FAILEDInstalling github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDEDInstalling github.com/acroca/go-symbols FAILEDInstalling github.com/ramya-rao-a/go-outline FAILEDInstalling golang.org/x/tools/cmd/guru FAILEDInstalling golang.org/x/tools/cmd/gorename FAILEDInstalling github.com/acroca/go-symbols FAILEDInstalling github.com/fatih/gomodifytags SUCCEEDEDInstalling github.com/haya14busa/goplay/cmd/goplay SUCCEEDEDInstalling golang.org/x/tools/cmd/guru FAILEDInstalling github.com/josharian/impl FAILEDInstalling golang.org/x/tools/cmd/gorename FAILEDInstalling github.com/rogpeppe/godef SUCCEEDEDInstalling github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDEDInstalling github.com/rogpeppe/godef SUCCEEDEDInstalling golang.org/x/tools/cmd/godoc FAILEDInstalling github.com/sqs/goreturns FAILEDInstalling github.com/sqs/goreturns FAILEDInstalling github.com/golang/lint/golint FAILEDInstalling github.com/golang/lint/golint FAILEDInstalling github.com/cweill/gotests/... FAILEDInstalling github.com/derekparker/delve/cmd/dlv SUCCEEDED 7 tools failed to install.
重新安装一次 在vs code 界面下 按F1快捷按键, 选择安装/升级工具 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528889988541.png?lastModify=1530111258 比上一次提示需要安装的包文件要多 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528890576571.png?lastModify=1530111258 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528890043930.png?lastModify=1530111258 由于没有科学上网,报错信息如下 Installing 16 tools at D:\GO_MyGo\bin gocode gopkgs go-outline go-symbols guru gorename gomodifytags goplay impl fillstruct godef godoc goreturns golint gotests dlv Installing github.com/mdempsky/gocode SUCCEEDEDInstalling github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDEDInstalling github.com/ramya-rao-a/go-outline FAILEDInstalling github.com/acroca/go-symbols FAILEDInstalling golang.org/x/tools/cmd/guru FAILEDInstalling golang.org/x/tools/cmd/gorename FAILEDInstalling github.com/fatih/gomodifytags SUCCEEDEDInstalling github.com/haya14busa/goplay/cmd/goplay SUCCEEDEDInstalling github.com/josharian/impl FAILEDInstalling github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDEDInstalling github.com/rogpeppe/godef SUCCEEDEDInstalling golang.org/x/tools/cmd/godoc FAILEDInstalling github.com/derekparker/delve/cmd/dlv FAILED 10 tools failed to install. 2.2.2手动安装go包文件由于之前有些包已经被成功安装 自动生成以下目录 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528893554872.png?lastModify=1530111258 进入自动生成的golang目录,没有则手工创建该目录 在该目录下打开cmd窗口 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528893716954.png?lastModify=1530111258 依次克隆Git上的代码到本地 代码如下 如果golang目录下已经有了lint目录, 则不需要运行第一行 全部克隆完成后如下文件 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528894085494.png?lastModify=1530111258 将下载的文件夹移动到 如下 固定路径, 如果没有该路径,则手工创建,文件夹名字不能改名 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528894176863.png?lastModify=1530111258 回到GO_MyGo文件夹下, 打开CMD窗口,手动安装如下代码 go install github.com/mdempsky/gocodego install github.com/ramya-rao-a/go-outlinego install github.com/acroca/go-symbolsgo install golang.org/x/tools/cmd/gurugo install golang.org/x/tools/cmd/gorenamego install github.com/josharian/implgo install golang.org/x/tools/cmd/godocgo install github.com/sqs/goreturnsgo install github.com/golang/lint/golintgo install github.com/cweill/gotests/gotestsgo install github.com/derekparker/delve/cmd/dlv
file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528894793449.png?lastModify=1530111258 在bin目录下会自动产生编译好的文件 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528894866299.png?lastModify=1530111258 2.3 编写测试代码package main import "fmt" func main() { fmt.Println("hello,世界")}
启动调试测试,测试成功 file://C:/Users/springadmin/Desktop/Go/go%E5%88%9D%E7%BA%A7.assets/1528895037265.png?lastModify=1530111258 2. Go的背景和环境变量介绍2.1 背景介绍2.2 Go的环境变量介绍 |