为了能够愉快地进行golang编程,我们需要安装以下几样东西:
我将按照这个顺序叙述整个安装过程
docker 其实是可选的,它可以把应用程序打包为可移植的、自给自足的容器。这样一来,你就可以在本地生成golang程序的docker镜像,直接扔到测试环境,便可以进行测试了,不需要再进行代码上传,环境配置等操作了。 如果你觉得暂时用不到,也可以先不装。
Homebrew有点类似于Linux操作系统中的apt-get(Ubuntu)、yum(yum),Mac的操作系统中使用它解决包依赖问题,套用官方的话来说:
使用 Homebrew 安装 Apple 没有预装但 你需要的东西。
在命令行输入以下指令
fabric:~ fabric$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
出现以下内容,安装成功
==> Installation successful!==> Homebrew has enabled anonymous aggregate user behaviour analytics.Read the analytics documentation (and how to opt-out) here: https://docs.brew.sh/Analytics.html==> Next steps:- Run `brew help` to get started- Further documentation: https://docs.brew.sh
==> Installation successful!
==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics.html
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh
fabric:~ fabric$ brew search go==> Formulaealgol68g go-jira gofabric8 goolabs gx-go mongodb@3.6arangodb go-statik goffice goose Hugo mongooseargon2 go@1.4 gollum gopass jfrog-cli-go pangobogofilter go@1.8 golo gor jpegoptim pangommcargo-completion go@1.9 gom goreleaser lego percona-server-mongodbcertigo goaccess gomplate gost lgogdownloader pygobjectcgoban goad goocanvas gosu libgosu pygobject3clingo gobby goofys gotags mongo-c-driver ringojsdjango-completion gobject-introspection google-authenticator-libpam goto mongo-cxx-driver spaceinvaders-goforego gobuster google-benchmark gource mongo-orchestration spigotfuego gocr google-java-format govendor mongodb svgognu-go gocryptfs google-sparsehash gowsdl mongodb@3.0 wegogo godep google-sql-tool gox mongodb@3.2 wireguard-gogo-bindata goenv googler gst-plugins-good mongodb@3.4
fabric:~ fabric$ brew search go
==> Formulae
algol68g go-jira gofabric8 goolabs gx-go mongodb@3.6
arangodb go-statik goffice goose Hugo mongoose
argon2 go@1.4 gollum gopass jfrog-cli-go pango
bogofilter go@1.8 golo gor jpegoptim pangomm
cargo-completion go@1.9 gom goreleaser lego percona-server-mongodb
certigo goaccess gomplate gost lgogdownloader pygobject
cgoban goad goocanvas gosu libgosu pygobject3
clingo gobby goofys gotags mongo-c-driver ringojs
django-completion gobject-introspection google-authenticator-libpam goto mongo-cxx-driver spaceinvaders-go
forego gobuster google-benchmark gource mongo-orchestration spigot
fuego gocr google-java-format govendor mongodb svgo
gnu-go gocryptfs google-sparsehash gowsdl mongodb@3.0 wego
go godep google-sql-tool gox mongodb@3.2 wireguard-go
go-bindata goenv googler gst-plugins-good mongodb@3.4
我们发现最新的有1.9可以使用
fabric:~ fabric$ brew install go@1.9Updating Homebrew...==> Downloading https://homebrew.bintray.com/bottles/go@1.9-1.9.7.high_sierra.bottle.tar.gz######################################################################## 100.0%==> Pouring go@1.9-1.9.7.high_sierra.bottle.tar.gz==> CaveatsA 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#GOPATHYou may wish to add the GOROOT-based install location to your PATH: export PATH=$PATH:/usr/local/opt/go@1.9/libexec/binThis formula is keg-only, which means it was not symlinked into /usr/local,because this is an alternate version of another formula.If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/go@1.9/bin:$PATH"' >> ~/.bash_profile==> Summary /usr/local/Cellar/go@1.9/1.9.7: 7,668 files, 294.2MB
fabric:~ fabric$ brew install go@1.9
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/go@1.9-1.9.7.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go@1.9-1.9.7.high_sierra.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@1.9/libexec/bin
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/go@1.9/bin:$PATH"' >> ~/.bash_profile
==> Summary
/usr/local/Cellar/go@1.9/1.9.7: 7,668 files, 294.2MB
fabric:~ fabric$ vim ~/.bashrc
将下面内容添加进上面的文件
#GOROOTexport GOROOT=/usr/local/opt/go\@1.9#GOPATHexport GOPATH=$HOME/Documents/code/gopath#GOPATH root binexport PATH=$PATH:$GOROOT/bin
#GOROOT
export GOROOT=/usr/local/opt/go\@1.9
#GOPATH
export GOPATH=$HOME/Documents/code/gopath
#GOPATH root bin
export PATH=$PATH:$GOROOT/bin
GOPATH可以根据个人习惯设置为其他目录 本人习惯在home目录下的Documents里新建一个code目录,用于存放各种语言的代码,比如Documents/code/gopath用于存放golang的代码,Documents/code/www用于存放php代码...
让改动生效
fabric:~ fabric$ source ~/.bashrc
fabric:~ fabric$ go envGOARCH="amd64"GOBIN=""GOEXE=""GOHOSTARCH="amd64"GOHOSTOS="darwin"GOOS="darwin"GOPATH="/Users/fabric/Documents/code/gopath"GORACE=""GOROOT="/usr/local/opt/go\@1.9"GOTOOLDIR="/usr/local/Cellar/go@1.9/1.9.7/libexec/pkg/tool/darwin_amd64"GCCGO="gccgo"CC="clang"GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wc/bby1pbz17v3dkr8rmcpjptwm0000gn/T/go-build871394220=/tmp/go-build -gno-record-gcc-switches -fno-common"CXX="clang++"CGO_ENABLED="1"CGO_CFLAGS="-g -O2"CGO_CPPFLAGS=""CGO_CXXFLAGS="-g -O2"CGO_FFLAGS="-g -O2"CGO_LDFLAGS="-g -O2"PKG_CONFIG="pkg-config"
fabric:~ fabric$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/fabric/Documents/code/gopath"
GORACE=""
GOROOT="/usr/local/opt/go\@1.9"
GOTOOLDIR="/usr/local/Cellar/go@1.9/1.9.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wc/bby1pbz17v3dkr8rmcpjptwm0000gn/T/go-build871394220=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
这个在我另外一篇文章做了详细的叙述,详情请见《修改并编译golang源码》
fabric:~ fabric$ brew install git==> Downloading https://homebrew.bintray.com/bottles/git-2.18.0.high_sierra.bott######################################################################## 100.0%==> Pouring git-2.18.0.high_sierra.bottle.tar.gz==> CaveatsBash completion has been installed to: /usr/local/etc/bash_completion.dzsh completions and functions have been installed to: /usr/local/share/zsh/site-functionsEmacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/git==> Summary /usr/local/Cellar/git/2.18.0: 1,488 files, 295.6MB
fabric:~ fabric$ brew install git
==> Downloading https://homebrew.bintray.com/bottles/git-2.18.0.high_sierra.bott
==> Pouring git-2.18.0.high_sierra.bottle.tar.gz
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git
/usr/local/Cellar/git/2.18.0: 1,488 files, 295.6MB
fabric:~ fabric$ git config user.namefabric:~ fabric$ git config user.email
fabric:~ fabric$ git config user.name
fabric:~ fabric$ git config user.email
fabric:~ fabric$ git config --global user.name "你的用户名"fabric:~ fabric$ git config --global user.email "你的邮箱地址"
fabric:~ fabric$ git config --global user.name "你的用户名"
fabric:~ fabric$ git config --global user.email "你的邮箱地址"
为了向github自己的仓库提交代码,我们需要设置SSH KEY
fabric:~ fabric$ ssh-keygen -t rsa -C 你的邮箱地址Generating public/private rsa key pair.Enter file in which to save the key (/Users/fabric/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/fabric/.ssh/id_rsa.Your public key has been saved in /Users/fabric/.ssh/id_rsa.pub.The key fingerprint is:SHA256:生成的密钥指纹 你的邮箱地址The key's randomart image is:+---[RSA 2048]----+生成的密钥randomart image +----[SHA256]-----+
fabric:~ fabric$ ssh-keygen -t rsa -C 你的邮箱地址
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fabric/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/fabric/.ssh/id_rsa.
Your public key has been saved in /Users/fabric/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:生成的密钥指纹 你的邮箱地址
The key's randomart image is:
+---[RSA 2048]----+
生成的密钥randomart image
+----[SHA256]-----+
fabric:~ fabric$ cat /Users/fabric/.ssh/id_rsa.pub 你的公钥
fabric:~ fabric$ cat /Users/fabric/.ssh/id_rsa.pub
你的公钥
github设置公钥的地方: 右上角头像下拉选项 -> Settings -> SSH and GPG keys
右上角头像下拉选项 -> Settings -> SSH and GPG keys
cd 你的代码目录git clone 你的代码git仓库地址
cd 你的代码目录
git clone 你的代码git仓库地址
Homebrew 的 Cask 已经支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 来进行安装:
fabric:~ fabric$ brew cask install docker
在载入 Docker app 后,点击 Next,可能会询问你的 macOS 登陆密码,你输入即可。之后会弹出一个 Docker 运行的提示窗口,状态栏上也有有个如下所示的小鲸鱼的图标:
验证一下是不是安装成功了呢,输入以下命令
fabric:~ fabric$ docker versionClient: Version: 18.06.0-ce API version: 1.38 Go version: go1.10.3 Git commit: 0ffa825 Built: Wed Jul 18 19:05:26 2018 OS/Arch: darwin/amd64 Experimental: falseServer: Engine: Version: 18.06.0-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.3 Git commit: 0ffa825 Built: Wed Jul 18 19:13:46 2018 OS/Arch: linux/amd64 Experimental: true
fabric:~ fabric$ docker version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:05:26 2018
OS/Arch: darwin/amd64
Experimental: false
Server:
Engine:
API version: 1.38 (minimum version 1.12)
Built: Wed Jul 18 19:13:46 2018
OS/Arch: linux/amd64
Experimental: true
如果需要手动下载,请点击以下链接下载 Stable 或 Edge 版本的 Docker for Mac。
如同 macOS 其它软件一样,安装也非常简单,双击下载的 .dmg 文件,然后将鲸鱼图标拖拽到 Application 文件夹即可。
由于一些大家都知道的原因,我们拉取Docker镜像会很缓慢,我们可以通过添加加速器来解决。比如网易的镜像地址:
http://hub-mirror.c.163.com
点击右上角的小鲸鱼图标 -> Perferences,然后选择下图中的Daemon->Registry mirrors,添加上面的地址
应用改动后,docker会重启,我们来看一下是否配置成功
fabric:~ fabric$ docker infoContainers: 0 Running: 0 ...Registry Mirrors: http://hub-mirror.c.163.com/Live Restore Enabled: false
fabric:~ fabric$ docker info
Containers: 0
Running: 0
...
Registry Mirrors:
http://hub-mirror.c.163.com/
Live Restore Enabled: false
可以看到,配置的地址已经生效了。
这里只是提供个工具推荐,安装Goland的文章网上一抓一大把,我就不赘述了。
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728