windows10 ,64位
1、安装 VSCode 编辑器Visual Studio Code 官网
官网下载即可
3.1安装
go语言官网提供了不同平台的安装。
如果无法访问可访问谷歌中国官网。
3.2 设置环境变量
GOROOTGOPATH
Path
添加如下三条
go env
go
3.3 创建 hello world!
gosrchello.go
package mainimport "fmt"func main() {fmt.Printf("hello, world\n")
}
在终端运行
go run hello.go
4、安装必要的工具和插件
4.1 安装 go 的一些工具
- 解决golang.org 连不上的问题
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
目录创建成功后,在x目录中执行
git clone https://github.com/golang/tools.git
安装工具包
执行
go install golang.org/x/tools/cmd/guru
报错
src\golang.org\x\tools\internal\imports\mod.go:17:2: cannot find package "golang.org/x/mod/module" in any of:D:\Go\src\golang.org\x\mod\module (from $GOROOT) E:\Workplace\go\src\golang.org\x\mod\module (from $GOPATH)
src\golang.org\x\tools\internal\gocommand\vendor.go:16:2: cannot find package "golang.org/x/mod/semver" in any of:D:\Go\src\golang.org\x\mod\semver (from $GOROOT) E:\Workplace\go\src\golang.org\x\mod\semver (from $GOPATH)
src\golang.org\x\tools\go\packages\golist.go:27:2: cannot find package "golang.org/x/xerrors" in any of:D:\Go\src\golang.org\x\xerrors (from $GOROOT)E:\Workplace\go\src\golang.org\x\xerrors (from $GOPATH)
解决办法
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/mod.git
git clone https://github.com/golang/xerrors.git
go install golang.org/x/tools/cmd/guru
- 安装其余插件
go install golang.org/x/tools/cmd/gorename
先go get 再 go install
go get -u -v github.com/rogpeppe/godef
go install github.com/rogpeppe/godef
go get -u -v github.com/ramya-rao-a/go-outline
go install github.com/ramya-rao-a/go-outline
go get -u -v github.com/acroca/go-symbols
go install github.com/acroca/go-symbols
go get -u -v github.com/josharian/impl
go install github.com/josharian/impl
go get -u -v github.com/sqs/goreturns
go install github.com/sqs/goreturns
go get -u -v github.com/golang/lint/golint
go install github.com/golang/lint/golint
go get -u -v github.com/cweill/gotests/gotests
go install github.com/cweill/gotests/gotests
go get -u -v github.com/nsf/gocode
go install github.com/nsf/gocode
查看bin文件夹,可以看到
安装成功
4.2 vscode 插件
- EXTENSION
转到->扩展go
5、安装与运行 go tour
先运行如下命令
go get github.com/Go-zh/tour/gotour
gotour
按照提示
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/tour.git
go build
报错
go: golang.org/x/tools@v0.0.0-20190312164927-7b79afddac43: Get "https://proxy.golang.org/golang.org/x/tools/@v/v0.0.0-20190312164927-7b79afddac43.mod": dial tcp 172.217.160.113:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
解决办法
在 PowerShell 中设置
$env:GOPROXY = "https://goproxy.io"
再次运行
go build
go install
6、我的第一个包与测试
- 创建包并进入该目录
$ mkdir $GOPATH/src/github.com/user/stringutil
$ cd $GOPATH/src/github.com/user/stringutil
reverse.go
// stringutil 包含有用于处理字符串的工具函数。
package stringutil// Reverse 将其实参字符串以符文为单位左右反转。
func Reverse(s string) string {r := []rune(s)for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {r[i], r[j] = r[j], r[i]}return string(r)
}
go buildgo installpkghello.go$GOPATH/src/github.com/user/hello
package mainimport ("fmt""github.com/user/stringutil"
)func main() {fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}
- 安装hello程序
无论是安装包还是二进制文件,go 工具都会安装它所依赖的任何东西。 因此当我们通过
cd $GOPATH/src/github.com/user/hello
go install
hellostringutil
- 运行
运行此程序的新版本,你应该能看到一条新的,反向的信息:
- 测试
$GOPATH/src/github.com/user/stringutil/reverse_test.go
package stringutilimport "testing"func TestReverse(t *testing.T) {cases := []struct {in, want string}{{"Hello, world", "dlrow ,olleH"},{"Hello, 世界", "界世 ,olleH"},{"", ""},}for _, c := range cases {got := Reverse(c.in)if got != c.want {t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)}}
}
go test
cd $GOPATH/src/github.com/user/stringutil
go test
7、问题或要点小结
buildcleandocenvbugfixfmtgenerategetinstalllistruntesttoolversionvet