golang
golang https://github.com/golang/go http://golang.org/ https://golang.org/doc/install https://go.googlesource.com/go 官方下载: https://golang.org/dl/ [macOS] https://storage.googleapis.com/golang/go1.7.1.darwin-amd64.pkg [linux] https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz [win64] https://storage.googleapis.com/golang/go1.7.1.windows-amd64.msi [source] https://storage.googleapis.com/golang/go1.7.1.src.tar.gz 国内下载: http://www.golangtc.com/download http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.darwin-amd64.pkg http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.darwin-amd64.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.freebsd-386.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.freebsd-amd64.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.linux-386.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.linux-amd64.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.src.tar.gz http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.windows-386.msi http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.windows-386.zip http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.windows-amd64.msi http://www.golangtc.com/static/go/1.7rc3/go1.7rc3.windows-amd64.zip http://pan.baidu.com/s/1hq1mrDM ---------------- 安装Go语言 ---------------- 方式一:利用管理工具(可能不包含go源码或者源码位置分开,导致工具不能使用,不建议作为开发使用) Homebrew (mac) $ brew install go --cross-compile-all $ ls `go env GOROOT`/pkg apt-get (ubuntu) $ apt-get install golang 方式二:官方二进制分发包(推荐) 支持平台众多,参见前面“国内下载”。 linux是tar.gz包手动解压到/usr/local/: $ sudo tar -C /usr/local -xzf go1.7rc3.linux-amd64.tar.gz mac平台的pkg向导也会将go安装在/usr/local/ windows平台的msi,会将GO安装C:盘(C:\Go),并且GOPATH为C:\Go\GOPATH,在“系统属性 - 高级 - 环境变量”中配置它们。 为mac和linux配置环境变量(GOROOT、PATH、GOPATH) GOROOT $ echo 'export GOROOT="/usr/local/go"' >> ~/.bash_profile $ echo 'export GOROOT="/usr/local/go"' >> ~/.bashrc # Ubuntu Desktop $ echo 'export GOROOT="/usr/local/go"' >> ~/.zshrc # Zsh PATH $ echo 'export PATH="$PATH:$GOROOT/bin"' >> ~/.bash_profile $ echo 'export PATH="$PATH:$GOROOT/bin"' >> ~/.bashrc # Ubuntu Desktop $ echo 'export PATH="$PATH:$GOROOT/bin"' >> ~/.zshrc # Zsh GOPATH 用于go查找、安装go包,可用英文冒号(windows是分号)分隔多个路径。 $ mkdir -p $HOME/Developer/gopath 然后 $ echo 'export GOPATH="$HOME/Developer/gopath"' >> ~/.bash_profile $ echo 'export GOPATH="$HOME/Developer/gopath"' >> ~/.bashrc # Ubuntu Desktop $ echo 'export GOPATH="$HOME/Developer/gopath"' >> ~/.zshrc # Zsh 如果将来需要使用 $GOPATH/bin 中的工具,可以将它也加入到PATH中, 或者将其中常用的命令创建文件软链接到PATH指向的目录(如/usr/local/bin)里。 安装完成 重启终端(或者用source重新载入你的启动配置文件) $ go version // 查看go版本 go version go1.7rc3 darwin/amd64 $ go env // 查看go环境 例子 创建一个测试文件 hello.go,内容: package main import "fmt" // main func main() { fmt.Println("Hello World!") } $ go run hello.go Hello World! $ go build hello.go $ ./hello Hello World! ---------------- 配置开发工具 ---------------- 使用go get安装: go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/golang/lint/golint go get -u -v github.com/lukehoban/go-outline go get -u -v sourcegraph.com/sqs/goreturns go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/tpng/gopkgs go get -u -v github.com/newhook/go-symbols go get -u -v golang.org/x/tools/cmd/guru go get -u -v github.com/alecthomas/gometalinter 使用gopm代替go get https://gopm.io/download 国内无法下载某些包的时候,可以在这里获得。 然后手动复制到$GOPATH/src对应的目录结构中,用 go install 命令安装。 有个更方便的工具gopm: https://github.com/gpmgo/gopm $ go get -u github.com/gpmgo/gopm (假设GOPATH只有一个目录) 如果常用到gopm,建议创建软链接到PATH指向的目录,如: $ ln -s $GOPATH/bin/gopm /usr/local/bin/gopm 否则可以临时设置PATH变量: $ export PATH="$PATH:$GOPATH/bin" C:\> start cmd /k path "%GOPATH%\bin";%PATH% 使用gopm get代替go get获得包: (注意:其中-g表示下载包到~/.gopm/repos后拷贝到$GOPATH/src中) gopm get -g -u -v github.com/nsf/gocode gopm get -g -u -v github.com/rogpeppe/godef gopm get -g -u -v github.com/golang/lint/golint gopm get -g -u -v github.com/lukehoban/go-outline gopm get -g -u -v sourcegraph.com/sqs/goreturns gopm get -g -u -v golang.org/x/tools/cmd/gorename gopm get -g -u -v github.com/tpng/gopkgs gopm get -g -u -v github.com/newhook/go-symbols gopm get -g -u -v golang.org/x/tools/cmd/guru gopm get -g -u -v github.com/alecthomas/gometalinter 然后使用go install安装包: (注意:如果使用go get就不需要再执行一次install,但是gopm需要) go install github.com/nsf/gocode go install github.com/rogpeppe/godef go install github.com/golang/lint/golint go install github.com/lukehoban/go-outline go install sourcegraph.com/sqs/goreturns go install golang.org/x/tools/cmd/gorename go install github.com/tpng/gopkgs go install github.com/newhook/go-symbols go install golang.org/x/tools/cmd/guru go install github.com/nsf/gocode go install github.com/alecthomas/gometalinter 有些包可能仍然无效(假如 goreturns),可以到github中搜索(找到 github.com/sqs/goreturns), 然后用go get命令安装,如果失败,用gopm get -g加go install命令安装它。 再不行,到 https://gopm.io/download 中搜索,手动解压复制到 $GOPATH/src 后,go install安装。 注:Windows下载安装某些64位程序可能需要64位版本的gcc支持。 下载MinGW64:http://sourceforge.net/projects/mingw-w64/files/latest/download?source=files 运行安装向导,在Settings页里选择Architecture为x86_64,在Installation folder页更改为C:\mingw-w64。 安装完成后,将环境变量PATH中加入C:\mingw-w64\mingw64\bin 然后go-sqlite3就能被下载和正确编译了: C:\> go get -u -v github.com/mattn/go-sqlite3 github.com/mattn/go-sqlite3 (download) github.com/mattn/go-sqlite3 golang tools go get -u -v golang.org/x/tools 如果不能连接,可以用github的镜像 go get -v github.com/golang/tools go get -v github.com/golang/net go get -v github.com/golang/crypto 然后创建目录在$GOPATH/golang.org/x,并将tools移动进去 mv $GOPATH/src/github.com/golang/net $GOPATH/src/golang.org/x/ mv $GOPATH/src/github.com/golang/crypto $GOPATH/src/golang.org/x/ 根据godoc的介绍用go install命令安装:https://godoc.org/golang.org/x/tools go install golang.org/x/tools/benchmark/parse go install golang.org/x/tools/blog go install golang.org/x/tools/blog/atom go install golang.org/x/tools/cmd/benchcmp go install golang.org/x/tools/cmd/bundle go install golang.org/x/tools/cmd/callgraph go install golang.org/x/tools/cmd/compilebench go install golang.org/x/tools/cmd/cover go install golang.org/x/tools/cmd/digraph go install golang.org/x/tools/cmd/eg go install golang.org/x/tools/cmd/fiximports go install golang.org/x/tools/cmd/godex go install golang.org/x/tools/cmd/godoc go install golang.org/x/tools/cmd/goimports go install golang.org/x/tools/cmd/gomvpkg go install golang.org/x/tools/cmd/gorename go install golang.org/x/tools/cmd/gotype go install golang.org/x/tools/cmd/goyacc go install golang.org/x/tools/cmd/guru go install golang.org/x/tools/cmd/guru/serial go install golang.org/x/tools/cmd/heapview go install golang.org/x/tools/cmd/html2article go install golang.org/x/tools/cmd/present go install golang.org/x/tools/cmd/ssadump go install golang.org/x/tools/cmd/stress go install golang.org/x/tools/cmd/stringer go install golang.org/x/tools/cmd/tip go install golang.org/x/tools/cmd/toolstash go install golang.org/x/tools/container/intsets go install golang.org/x/tools/cover go install golang.org/x/tools/go/ast/astutil go install golang.org/x/tools/go/buildutil go install golang.org/x/tools/go/callgraph go install golang.org/x/tools/go/callgraph/cha go install golang.org/x/tools/go/callgraph/rta go install golang.org/x/tools/go/callgraph/static go install golang.org/x/tools/godoc go install golang.org/x/tools/godoc/analysis go install golang.org/x/tools/godoc/redirect go install golang.org/x/tools/godoc/static go install golang.org/x/tools/godoc/util go install golang.org/x/tools/godoc/vfs go install golang.org/x/tools/godoc/vfs/gatefs go install golang.org/x/tools/godoc/vfs/httpfs go install golang.org/x/tools/godoc/vfs/mapfs go install golang.org/x/tools/godoc/vfs/zipfs go install golang.org/x/tools/go/gccgoexportdata go install golang.org/x/tools/go/gcexportdata go install golang.org/x/tools/go/gcimporter15 go install golang.org/x/tools/go/internal/gccgoimporter go install golang.org/x/tools/go/loader go install golang.org/x/tools/go/pointer go install golang.org/x/tools/go/ssa go install golang.org/x/tools/go/ssa/interp go install golang.org/x/tools/go/ssa/ssautil go install golang.org/x/tools/go/types/typeutil go install golang.org/x/tools/go/vcs go install golang.org/x/tools/imports go install golang.org/x/tools/playground go install golang.org/x/tools/playground/socket go install golang.org/x/tools/present go install golang.org/x/tools/refactor/eg go install golang.org/x/tools/refactor/importgraph go install golang.org/x/tools/refactor/rename go install golang.org/x/tools/refactor/satisfy 备份$gopath/src(排除m2nlight目录) 7za a -mx=9 ~/Desktop/gopath_`date +%Y-%m-%d-%H-%M-%S`_src.7z $GOPATH/src -xr\!m2nlight/ 在桌面生成文件如:gopath_2016-07-31-11-26-51_src.7z TextMate 设置中将Variables的PATH设置为:/usr/local/bin:$PATH 用TextMate打开hello.go文件,它会提示安装go插件,也可以手动在 设置 - Bundles 中搜索 go 进行安装。 按CMD+R查看编译运行结果。 Sublime Text 3 所有扩展的配置,都是在对应的配置文件中指定GOPATH和(或)GOROOT 安装扩展包:GoSublime、Golang Build和Golang Tools Integration [1]配置GoSublime: Sublime Text - Preferences - Package Settings - GoSublime - Settings User { "env": { "GOROOT": "/usr/local/go", "GOPATH": "/Users/yourname/Developer/gopath" } } 注意GOPATH路径:mac是/Users/yourname,linux是/home/yourname,windows是C:\\Go\\GOPATH windows的GOROOT路径是C:\\Go GoSublime技巧:按CMD+..或者Ctrl+..显示GoSublime的快捷键列表。 [2]配置Golang Build: Sublime Text - Preferences - Package Settings - Golang Config - Settings User { "PATH": "/usr/local/go/bin", "GOROOT": "/usr/local/go", "GOPATH": "/Users/YOURNAME/Developer/gopath" } 注意GOPATH路径:mac是/Users/yourname,linux是/home/yourname,windows是C:\\Go\\GOPATH windows的GOROOT路径是C:\\Go,PATH路径是C:\\Go\\bin 按CMD+B,选择Go - Run查看结果。CMD+SHIFT+B可重新选择编译条件。 [3]关于Golang Tools Integration: 安装工具:(粘贴到终端) go get github.com/nsf/gocode go get github.com/golang/lint/golint go get golang.org/x/tools/cmd/guru go get golang.org/x/tools/cmd/goimports go get golang.org/x/tools/cmd/gorename 以及保存时格式化、代码着色增强等。 VSCode 安装Go扩展,如果能访问golang,可以用一条命令安装全部工具: 按Ctrl+Shift+P输入:Go:Install/Update Tools go get -u -v github.com/nsf/gocode go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs go get -u -v github.com/ramya-rao-a/go-outline go get -u -v github.com/acroca/go-symbols go get -u -v golang.org/x/tools/cmd/guru go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/fatih/gomodifytags go get -u -v github.com/haya14busa/goplay/cmd/goplay go get -u -v github.com/josharian/impl go get -u -v github.com/rogpeppe/godef go get -u -v golang.org/x/tools/cmd/godoc go get -u -v sourcegraph.com/sqs/goreturns go get -u -v github.com/golang/lint/golint go get -u -v github.com/cweill/gotests go get -u -v github.com/derekparker/delve/cmd/dlv go install github.com/nsf/gocode go install github.com/uudashr/gopkgs/cmd/gopkgs go install github.com/ramya-rao-a/go-outline go install github.com/acroca/go-symbols go install golang.org/x/tools/cmd/guru go install golang.org/x/tools/cmd/gorename go install github.com/fatih/gomodifytags go install github.com/haya14busa/goplay/cmd/goplay go install github.com/josharian/impl go install github.com/rogpeppe/godef go install golang.org/x/tools/cmd/godoc go install sourcegraph.com/sqs/goreturns go install github.com/golang/lint/golint go install github.com/cweill/gotests go install github.com/derekparker/delve/cmd/dlv All tools successfully installed. You're ready to Go :). 调试工具支持(可选安装,详细见Go扩展) 安装delve(https://github.com/derekparker/delve/tree/master/Documentation/installation) ### mac ### $ brew install go-delve/delve/delve $ dlv version Delve Debugger Version: 0.11.0-alpha Build: v0.11.0 注意:homebrew安装不需要手动创建证书dlv-cert,它会被自动创建,但是删除的时候需要手动删除dev-cert证书: $ brew uninstall delve $ sudo security delete-certificate -t -c dlv-cert /Library/Keychains/System.keychain ### linux ### $ go get -v github.com/derekparker/delve/cmd/dlv $ dlv version ### windows ### C:\> go get github.com/derekparker/delve/cmd/dlv C:\> C:\go\gopath\bin\dlv.exe version 回到VSCode,点击左侧的调试功能,在调试窗格右上角点设置,从列表里选择Go 会出现launch.json配置文件,输入配置如下: { "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}", "env": {}, "args": [] }, { "name": "Remote", "type": "go", "request": "launch", "mode": "remote", "remotePath": "${workspaceRoot}", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}", "env": {}, "args": [] } ] } 以上配置了两个调试功能,Launch和Remote 以Remote为例 在工作目录运行远程调试命令: $ dlv debug --headless --listen=:2345 --log 然后在VSCode的Debug窗格里选择Remote。 设置断点,按F5开始调试。(SHIFT+F5结束调试) ------------ 跨平台编译 ------------ (1)未安装gox情况 如果没使用cgo,go本身就支持跨平台编译,设置GOOS和GOARCH即可。 ------------------------------------------------------ GOOS GOARCH OS version ------------------------------------------------------ linux 386/amd64/arm >= Linux 2.6 darwin 386/amd64 OS X (Snow Leopard + Lion) freebsd 386/amd64 >= FreeBSD 7 windows 386/amd64 >= Windows 2000 ------------------------------------------------------- Mac下编译Win64: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go Linux下编译Win64: CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go Win下编译Mac: SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go build main.go Win下编译Linux 64: SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build main.go (2)安装gox情况 $ go install github.com/mitchellh/gox $ gox -h //查看帮助 直接在当前目录按{{.Dir}}_{{.OS}}_{{.Arch}}格式生成所有跨平台版本的程序: $ cd $GOPATH/src/myproj $ gox 通过output可指定输出模板,-os可指定操作系统,如下: $ gox -os="windows darwin linux" -output="./bin/{{.OS}}_{{.Arch}}/{{.Dir}}" ------------ vendor 管理 ------------ 安装govendor(https://github.com/kardianos/govendor) 它能方便的管理包路径 $ go get -u -v github.com/kardianos/govendor $ govendor -version v1.0.9 用法: $ cd $GOPATH/src/myproj $ govendor init # 在项目文件夹产生 vendor/vendor.json $ govendor add +external # 添加$GOPATH中的依赖包到 vendor $ govendor update +vendor # 从$GOPATH中更新依赖包到 vendor $ govendor fetch +out # 从网络拉取依赖的包 # 拉取指定版本的包 $ govendor fetch golang.org/x/net/context@a4bbce9fcae005b22ae5443f6af064d80a6f5a55 $ govendor fetch golang.org/x/net/context@v1 # Get latest v1.*.* tag or branch. $ govendor fetch golang.org/x/net/context@=v1 # Get the tag or branch named "v1". $ govendor fetch golang.org/x/net/context # 更新包 $ govendor list # 显示包,可以用 -v fmt 参数查看某个包的详细依赖 $ govendor update -uncommitted <updated-package-import-path> # 仅仅测试被改变的依赖包,但不真正更新 vendor 编辑 vendor/vendor.json 文件,在ignore字段可加入忽略的包,用空格分割,如:"ignore": "test appengine" $ govendor fmt +local # Format your repository only $ govendor install +local # Build everything in your repository only $ govendor test +local # Test your repository only 说明: +local l 本地包,即项目自身的包组织 +external e 外部包,即被 $GOPATH 管理,但不在 vendor 目录下 +vendor v 已被 govendor 管理,即在 vendor 目录下 +std s 标准库中的包 +unused u 未使用的包,即包在 vendor 目录下,但项目并没有用到 +missing m 代码引用了依赖包,但该包并没有找到 +program p 主程序包,意味着可以编译为执行文件 +outside 外部包和缺失的包 +all 所有的包 命令: init 初始化 vendor 目录 list 列出所有的依赖包 add 添加包到 vendor 目录,如 govendor add +external 添加所有外部包 add PKG_PATH 添加指定的依赖包到 vendor 目录 update 从 $GOPATH 更新依赖包到 vendor 目录 remove 从 vendor 管理中删除依赖 status 列出所有缺失、过期和修改过的包 fetch 添加或更新包到本地 vendor 目录 sync 本地存在 vendor.json 时候拉去依赖包,匹配所记录的版本 get 类似 go get 目录,拉取依赖包到 vendor 目录