Mac:
* 下载go安装包
** 通过源码编译安装需要先安装go1.4
wget https://studygolang.com/dl/golang/go1.10.3.src.tar.gz
tar 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.pkg
if [ ! -d /opt ]; then mkdir /opt; else echo "Directory /opt already exists"; fi
sudo 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/bin
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
# export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
export CLASSPATH=${JAVA_HOME}/lib/
export GOROOT=/usr/local/Cellar/go/1.10.3/libexec
export GOBIN=${GOROOT}/bin
export GOPATH=${HOME}/Code/golang
export 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 colourful
export CLICOLOR=1
export LSCOLORS=Exfxcxdxbxegedabagacad
# Tell grep to highlight matches
export 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.zip
unzip liteidex32.1.macosx-qt4.zip -d .
sudo mv liteide /opt/
sudo chown -hR `whoami`:staff /opt/liteide/
sed -n '/^export PATH=.*$/p' ~/.bash_profile
echo 'export PATH=${PATH}:/opt/liteide/LiteIDE.app/Contents/MacOS' >> ~/.bash_profile
. ~/.bash_profile
which liteIDE
liteIDE &
* 安装gdb [optional]
brew install gdb
which gdb
# /usr/local/bin/gdb
* 配置liteIDE system.env
# launch liteIDE
/opt/liteide/LiteIDE.app/Contents/MacOS/LiteIDE
View => Edit Current Environment
# native compiler darwin amd64
export GOROOT=/usr/local/Cellar/go/1.10.3/libexec
export GOBIN=${GOROOT}/bin
export GOPATH=${HOME}/Code/golang
GOARCH=amd64
GOOS=darwin
CGO_ENABLED=1
PATH=$GOBIN:$GOPATH:/bin:$PATH
LITEIDE_GDB=/usr/local/bin/gdb
LITEIDE_MAKE=make
LITEIDE_TERM=/usr/bin/open
LITEIDE_TERMARGS=-a Terminal
LITEIDE_EXEC=/usr/X11R6/bin/xterm
LITEIDE_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/gopm
ls $GOBIN
# go godoc gofmt gopm
# gopm安装成功
用gopm安装 golang.org/x/blog
if [ ! -d $GOPATH/bin ]; then mkdir $GOPATH/bin; fi
gopm 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/gopm
go build .
go install
gopm 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 -SIGTERM
godoc -http=:6060
可以查看手册了 http://localhost:6060/blog/go-slices-usage-and-internals
* 写一个字符串反转的例子
参考手册 http://zhanghum:6060/pkg/strings/#Split
(zhanghum是我的主机名)
// String project main.go
package main
import (
"fmt"
"strings"
)
func strrev(s string) string {
a := strings.Split(s, "")
i := 0
j := len(a) - 1
for i < j {
tmp := a[i]
a[i] = a[j]
a[j] = tmp
i++
j--
}
return strings.Join(a, "")
}
func main() {
s := strrev("Hello world!")
fmt.Println(s)
}
output:
C:/go/bin/go.exe build -i [J:/gocode/src/String]
成功: 进程退出代码 0.
J:/gocode/src/String/String.exe [J:/gocode/src/String]
!dlrow olleH
成功: 进程退出代码 0.