Mac:
* 下載go安裝包
go語言中文網
** 通過源碼編譯安裝需要先安裝go1.4
wget https://studygolang.com/dl/golang/go1.10.3.src.tar.gztar xvzf go1.10.3.src.tar.gz -C .cd ./go/src/./make.bash
Building Go cmd/dist using /Users/Mch/go1.4.
ERROR: Cannot find /Users/Mch/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
** 直接用安裝包安裝
wget https://studygolang.com/dl/golang/go1.10.3.darwin-amd64.pkgif [ ! -d /opt ]; then mkdir /opt; else echo "Directory /opt already exists"; fisudo installer -pkg go1.10.3.darwin-amd64.pkg -target /opt # 這裡出錯了 沒解決
** 用brew安裝
brew install go
-----------------------------------------------------------------------------------
==> Downloading https://homebrew.bintray.com/bottles/go-1.10.3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go-1.10.3.el_capitan.bottle.tar.gz
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
/usr/local/Cellar/go/1.10.3: 8,170 files, 336.8MB
-----------------------------------------------------------------------------------
* 配置環境變數 GOROOT, GOBIN, PATH, GOPATH
emacs -nw ~/.bash_profile
~/.bash_profile 檔案如下:
export MAVEN=/Users/Mch/apache-maven-3.5.0/binexport JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home# export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Homeexport CLASSPATH=${JAVA_HOME}/lib/export GOROOT=/usr/local/Cellar/go/1.10.3/libexecexport GOBIN=${GOROOT}/binexport GOPATH=${HOME}/Code/golangexport PATH=/Applications/XAMPP/bin:${GOBIN}:${PATH}:${JAVA_HOME}/bin:$MAVEN:/usr/local/apache-tomcat-9.0.0.M26/bin:~/.composer/vendor/bin:/opt/gradle-3.5.1/bin# Tell ls to be colourfulexport CLICOLOR=1export LSCOLORS=Exfxcxdxbxegedabagacad# Tell grep to highlight matchesexport GREP_OPTIONS='--color=auto'# export PATH="$HOME/.jenv/bin:$PATH"# eval "$(jenv init -)"
# 使配置生效. ~/.bash_profile# 查看版本go version
go version go1.10.3 darwin/amd64
* 下載liteide
golang中國 https://www.golangtc.com/download/liteide
wget https://www.golangtc.com/static/liteide/X32.1/liteidex32.1.macosx-qt4.zipunzip liteidex32.1.macosx-qt4.zip -d .sudo mv liteide /opt/sudo chown -hR `whoami`:staff /opt/liteide/sed -n '/^export PATH=.*$/p' ~/.bash_profileecho 'export PATH=${PATH}:/opt/liteide/LiteIDE.app/Contents/MacOS' >> ~/.bash_profile. ~/.bash_profilewhich liteIDEliteIDE &
* 安裝gdb [optional]
brew install gdbwhich gdb# /usr/local/bin/gdb
* 配置liteIDE system.env
# launch liteIDE/opt/liteide/LiteIDE.app/Contents/MacOS/LiteIDE
View => Edit Current Environment
# native compiler darwin amd64export GOROOT=/usr/local/Cellar/go/1.10.3/libexecexport GOBIN=${GOROOT}/binexport GOPATH=${HOME}/Code/golangGOARCH=amd64GOOS=darwinCGO_ENABLED=1PATH=$GOBIN:$GOPATH:/bin:$PATHLITEIDE_GDB=/usr/local/bin/gdbLITEIDE_MAKE=makeLITEIDE_TERM=/usr/bin/openLITEIDE_TERMARGS=-a TerminalLITEIDE_EXEC=/usr/X11R6/bin/xtermLITEIDE_EXECOPT=-e
* 測試安裝環境
File => New... => Go1 Command Project => Name: Greedy => OK
對於預設的輸出helloworld的項目
按 command + R, 編譯並運行
* 啟動godoc api文檔服務
godoc -http=:6060 &
用瀏覽器開啟 http://localhost:6060/ 一些參考手冊連結打不開
go get golang.org/x/blog
package golang.org/x/blog: unrecognized import path "golang.org/x/blog" (https fetch: Get https://golang.org/x/blog?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
部落格下載逾時,需要安裝gopm
go get -u github.com/gpmgo/gopmls $GOBIN# go godoc gofmt gopm# gopm安裝成功
用gopm安裝 golang.org/x/blog
if [ ! -d $GOPATH/bin ]; then mkdir $GOPATH/bin; figopm bin -d $GOPATH/bin golang.org/x/blog
output:
[GOPM] 07-29 11:50:20 [ WARN] Getting imports: no buildable Go source files in /var/folders/60/qw4hdqc50m32mwxr054b9j200000gn/T/359841253/src/golang.org/x/tools[GOPM] 07-29 11:50:20 [ WARN] Getting imports: no buildable Go source files in /Users/Mch/Code/golang/.vendor/src/golang.org/x/tools[GOPM] 07-29 11:50:21 [FATAL] Previous steps weren't successful or the project does not contain main package
出錯了
cd $GOPATH/src/github.com/github.com/gpmgo/gopmgo build .go installgopm install golang.org/x/blog # 這個錯誤
[GOPM] 07-29 12:00:54 [FATAL] fail to list imports: fail to get imports(/Users/Mch/Code/golang): import "/Users/Mch/Code/golang": cannot import absolute path
rm -f $GOBIN/gopm
再將$GOPATH/bin添加到PATH環境變數
which gopm
# /Users/Mch/Code/golang/bin/gopm
# 把gopm安裝到$GOPATH/bin, 再試
gopm get -g golang.org/x/blog # go get golang.org/x/blog 用gopm替換go
重啟godoc服務
lsof -i:6060 | awk '{if(NR>1) {print $2}}' | xargs kill -SIGTERMgodoc -http=:6060
可以查看手冊了 http://localhost:6060/blog/go-slices-usage-and-internals