经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
Mac os安装golang开发环境
来源:cnblogs  作者:亚楠老猎人  时间:2018/10/25 9:30:03  对本文有异议

为了能够愉快地进行golang编程,我们需要安装以下几样东西:

  • 包管理Homebrew
  • 语言环境golang
  • 版本管理git
  • 虚拟器docker
  • 编译器Goland

我将按照这个顺序叙述整个安装过程

docker 其实是可选的,它可以把应用程序打包为可移植的、自给自足的容器。这样一来,你就可以在本地生成golang程序的docker镜像,直接扔到测试环境,便可以进行测试了,不需要再进行代码上传,环境配置等操作了。
如果你觉得暂时用不到,也可以先不装。

1、安装brew


HomeBrew图标

Homebrew有点类似于Linux操作系统中的apt-get(Ubuntu)、yum(yum),Mac的操作系统中使用它解决包依赖问题,套用官方的话来说:

Homebrew 能干什么?

使用 Homebrew 安装 Apple 没有预装但 你需要的东西

让我们开始安装吧!

在命令行输入以下指令

  1. fabric:~ fabric$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

出现以下内容,安装成功

  1. ==> Installation successful!
  2. ==> Homebrew has enabled anonymous aggregate user behaviour analytics.
  3. Read the analytics documentation (and how to opt-out) here:
  4. https://docs.brew.sh/Analytics.html
  5. ==> Next steps:
  6. - Run `brew help` to get started
  7. - Further documentation:
  8. https://docs.brew.sh

2、安装并配置golang


golang图标

2.1 方法一:通过brew安装golang

  • 首先看看有哪些golang版本可用
  1. fabric:~ fabric$ brew search go
  2. ==> Formulae
  3. algol68g go-jira gofabric8 goolabs gx-go mongodb@3.6
  4. arangodb go-statik goffice goose Hugo mongoose
  5. argon2 go@1.4 gollum gopass jfrog-cli-go pango
  6. bogofilter go@1.8 golo gor jpegoptim pangomm
  7. cargo-completion go@1.9 gom goreleaser lego percona-server-mongodb
  8. certigo goaccess gomplate gost lgogdownloader pygobject
  9. cgoban goad goocanvas gosu libgosu pygobject3
  10. clingo gobby goofys gotags mongo-c-driver ringojs
  11. django-completion gobject-introspection google-authenticator-libpam goto mongo-cxx-driver spaceinvaders-go
  12. forego gobuster google-benchmark gource mongo-orchestration spigot
  13. fuego gocr google-java-format govendor mongodb svgo
  14. gnu-go gocryptfs google-sparsehash gowsdl mongodb@3.0 wego
  15. go godep google-sql-tool gox mongodb@3.2 wireguard-go
  16. go-bindata goenv googler gst-plugins-good mongodb@3.4

我们发现最新的有1.9可以使用

  • 安装brew下最新版本的go
  1. fabric:~ fabric$ brew install go@1.9
  2. Updating Homebrew...
  3. ==> Downloading https://homebrew.bintray.com/bottles/go@1.9-1.9.7.high_sierra.bottle.tar.gz
  4. ######################################################################## 100.0%
  5. ==> Pouring go@1.9-1.9.7.high_sierra.bottle.tar.gz
  6. ==> Caveats
  7. A valid GOPATH is required to use the `go get` command.
  8. If $GOPATH is not specified, $HOME/go will be used by default:
  9. https://golang.org/doc/code.html#GOPATH
  10. You may wish to add the GOROOT-based install location to your PATH:
  11. export PATH=$PATH:/usr/local/opt/go@1.9/libexec/bin
  12. This formula is keg-only, which means it was not symlinked into /usr/local,
  13. because this is an alternate version of another formula.
  14. If you need to have this software first in your PATH run:
  15. echo 'export PATH="/usr/local/opt/go@1.9/bin:$PATH"' >> ~/.bash_profile
  16. ==> Summary
  17. /usr/local/Cellar/go@1.9/1.9.7: 7,668 files, 294.2MB
  • 配置golang的相关环境变量
  1. fabric:~ fabric$ vim ~/.bashrc

将下面内容添加进上面的文件

  1. #GOROOT
  2. export GOROOT=/usr/local/opt/go\@1.9
  3. #GOPATH
  4. export GOPATH=$HOME/Documents/code/gopath
  5. #GOPATH root bin
  6. export PATH=$PATH:$GOROOT/bin

GOPATH可以根据个人习惯设置为其他目录
本人习惯在home目录下的Documents里新建一个code目录,用于存放各种语言的代码,比如Documents/code/gopath用于存放golang的代码,Documents/code/www用于存放php代码...

让改动生效

  1. fabric:~ fabric$ source ~/.bashrc
  • 试一试golang是否安装成功
    出现以下内容,则安装成功
  1. fabric:~ fabric$ go env
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOEXE=""
  5. GOHOSTARCH="amd64"
  6. GOHOSTOS="darwin"
  7. GOOS="darwin"
  8. GOPATH="/Users/fabric/Documents/code/gopath"
  9. GORACE=""
  10. GOROOT="/usr/local/opt/go\@1.9"
  11. GOTOOLDIR="/usr/local/Cellar/go@1.9/1.9.7/libexec/pkg/tool/darwin_amd64"
  12. GCCGO="gccgo"
  13. CC="clang"
  14. 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"
  15. CXX="clang++"
  16. CGO_ENABLED="1"
  17. CGO_CFLAGS="-g -O2"
  18. CGO_CPPFLAGS=""
  19. CGO_CXXFLAGS="-g -O2"
  20. CGO_FFLAGS="-g -O2"
  21. CGO_LDFLAGS="-g -O2"
  22. PKG_CONFIG="pkg-config"

2.2 方法二:从源码安装golang

这个在我另外一篇文章做了详细的叙述,详情请见《修改并编译golang源码》

3、安装配置git


git图标

3.1 用brew安装git

  1. fabric:~ fabric$ brew install git
  2. ==> Downloading https://homebrew.bintray.com/bottles/git-2.18.0.high_sierra.bott
  3. ######################################################################## 100.0%
  4. ==> Pouring git-2.18.0.high_sierra.bottle.tar.gz
  5. ==> Caveats
  6. Bash completion has been installed to:
  7. /usr/local/etc/bash_completion.d
  8. zsh completions and functions have been installed to:
  9. /usr/local/share/zsh/site-functions
  10. Emacs Lisp files have been installed to:
  11. /usr/local/share/emacs/site-lisp/git
  12. ==> Summary
  13. /usr/local/Cellar/git/2.18.0: 1,488 files, 295.6MB

3.2 配置git

3.2.1 查看用户名和邮箱地址

  1. fabric:~ fabric$ git config user.name
  2. fabric:~ fabric$ git config user.email

3.2.2 修改用户名和邮箱地址

  1. fabric:~ fabric$ git config --global user.name "你的用户名"
  2. fabric:~ fabric$ git config --global user.email "你的邮箱地址"

3.2.3 生成SSH KEY

为了向github自己的仓库提交代码,我们需要设置SSH KEY

  • 首先来生成SSH KEY
  1. fabric:~ fabric$ ssh-keygen -t rsa -C 你的邮箱地址
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/Users/fabric/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /Users/fabric/.ssh/id_rsa.
  7. Your public key has been saved in /Users/fabric/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:生成的密钥指纹 你的邮箱地址
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. 生成的密钥randomart image
  13. +----[SHA256]-----+
  • 复制上一步生成的公钥(public key),复制进github
  1. fabric:~ fabric$ cat /Users/fabric/.ssh/id_rsa.pub
  2. 你的公钥
github设置公钥的地方:

右上角头像下拉选项 -> Settings -> SSH and GPG keys

设置公钥

  • 可以试着在代码目录下拉取自己的仓库试试是否生效
  1. cd 你的代码目录
  2. git clone 你的代码git仓库地址

4、安装docker


docker

Homebrew 的 Cask 已经支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 来进行安装:

4.1 用brew安装docker

  1. fabric:~ fabric$ brew cask install docker

在载入 Docker app 后,点击 Next,可能会询问你的 macOS 登陆密码,你输入即可。之后会弹出一个 Docker 运行的提示窗口,状态栏上也有有个如下所示的小鲸鱼的图标:
docker图标

验证一下是不是安装成功了呢,输入以下命令

  1. fabric:~ fabric$ docker version
  2. Client:
  3. Version: 18.06.0-ce
  4. API version: 1.38
  5. Go version: go1.10.3
  6. Git commit: 0ffa825
  7. Built: Wed Jul 18 19:05:26 2018
  8. OS/Arch: darwin/amd64
  9. Experimental: false
  10. Server:
  11. Engine:
  12. Version: 18.06.0-ce
  13. API version: 1.38 (minimum version 1.12)
  14. Go version: go1.10.3
  15. Git commit: 0ffa825
  16. Built: Wed Jul 18 19:13:46 2018
  17. OS/Arch: linux/amd64
  18. Experimental: true

4.2 下载app安装

如果需要手动下载,请点击以下链接下载 Stable 或 Edge 版本的 Docker for Mac。

如同 macOS 其它软件一样,安装也非常简单,双击下载的 .dmg 文件,然后将鲸鱼图标拖拽到 Application 文件夹即可。
拖拽安装

4.3 给咱的docker提提速

由于一些大家都知道的原因,我们拉取Docker镜像会很缓慢,我们可以通过添加加速器来解决。比如网易的镜像地址:

  1. http://hub-mirror.c.163.com

点击右上角的小鲸鱼图标 -> Perferences,然后选择下图中的Daemon->Registry mirrors,添加上面的地址
增加仓库

应用改动后,docker会重启,我们来看一下是否配置成功

  1. fabric:~ fabric$ docker info
  2. Containers: 0
  3. Running: 0
  4. ...
  5. Registry Mirrors:
  6. http://hub-mirror.c.163.com/
  7. Live Restore Enabled: false

可以看到,配置的地址已经生效了。

5、安装Goland


Goland

这里只是提供个工具推荐,安装Goland的文章网上一抓一大把,我就不赘述了。

如此一来,整个安装过程便完成了!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号