59

golang 开发环境 配置

 5 years ago
source link: https://studygolang.com/articles/13868?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Mac:

* 下载go安装包

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

:beer: /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

Er2mmiV.png!web

对于默认的输出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 # 这个错误:x:

[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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK