我的安装信息如下:
PS C:\Users\z15075> go env set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\z15075\AppData\Local\go-build set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOOS=windows set GOPATH=C:\Users\z15075\go set GOPROXY= set GORACE= set GOROOT=C:\Go set GOTMPDIR= set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64 set GCCGO=gccgo set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD= set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\z15075\AppData\Local\Temp\go-build2328876 38=/tmp/go-build -gno-record-gcc-switches PS C:\Users\z15075>由于没有设置GOPATH环境变量,所以会使用默认的路径作为GOPATH:
PS C:\Users\z15075\go> pwd Path ---- C:\Users\z15075\go工作空间是一个目录,包含两个子目录:
src 包含go源文件,每个子目录以git仓库形式组织管理bin 包含可执行命令,go工具安装后的二进制文件使用goland开发工具,新建go工程,location为:
C:\Users\z15075\go\src\github.com\user\hello创建hello.go源文件,内容为:
package main import "fmt" func main() { fmt.Println("Hello, world.") }编译或安装,可看到bin目录下有hello.exe文件。
go install使用git bash或者goland vcs将该项目加入git管理:
git init git add * git commit -m "initial commit"在github新建仓库hello,在本地添加该远程仓库,并push
git remote add origin https://github.com/zhabit/hello.git git push -u origin master使用goland开发工具,新建go工程,location为:
C:\Users\z15075\go\src\github.com\user\stringutil创建reverse.go源文件,内容为:
package stringutil 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) }创建reverse_test.go源文件,测试Reverse方法,内容为:
package stringutil import "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构建:
go buildgo build构建时,会把结果输出到go build cache目录:
set GOCACHE=C:\Users\z15075\AppData\Local\go-build在hello项目中引入该库:
package main import ( "fmt" "github.com/user/stringutil" ) func main() { //fmt.Println("Hello,World!") fmt.Println(stringutil.Reverse("!oG ,olleH")) }