Mac Golang开发环境搭建 - Go语言中文社区

Mac Golang开发环境搭建


由于需要进行go语言项目的开发,基于自己个人的Mac电脑做了一些开发环境的配置,本文旨在做一个简单的记录。

先介绍下本文的电脑配置:


image.png

一、安装brew

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

让我们开始安装吧!

在命令行输入以下指令

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

二、安装并配置golang

首先看看有哪些golang版本可用

louxiujun@IT-C02Z9057LVDL  /usr/local/opt  brew search go
==> Formulae
algol68g                                 google-java-format
anycable-go                              google-sparsehash
arangodb                                 google-sql-tool
argon2                                   googler
aws-google-auth                          goolabs
bogofilter                               goose
cargo-completion                         gopass
certigo                                  gor
cgoban                                   goreleaser
clingo                                   goreman
django-completion                        gost
forego                                   gosu
fuego                                    gotags
gnu-go                                   goto
go                                       gource
go-bindata                               govc
go-jira                                  govendor
go-md2man                                gowsdl
go-statik                                gox
go@1.10 ✔                                gst-plugins-good
go@1.11                                  gx-go
go@1.12                                  hugo
go@1.9                                   jfrog-cli-go
goaccess                                 jpegoptim
goad                                     lego
gobby                                    lgogdownloader
gobject-introspection                    libgosu
gobuster                                 mongo-c-driver
gocr                                     mongo-cxx-driver
gocryptfs                                mongo-orchestration
godep                                    mongoose
goenv                                    pango
gofabric8                                pangomm
goffice                                  protoc-gen-go
golang-migrate                           pygobject
gollum                                   pygobject3
golo                                     ringojs
gom                                      spaceinvaders-go
gomplate                                 spigot
goocanvas                                svgo
goofys                                   wego
google-authenticator-libpam              wireguard-go
google-benchmark                         write-good

==> Casks
homebrew/cask-fonts/font-go
homebrew/cask-fonts/font-go-medium
homebrew/cask-fonts/font-go-mono
homebrew/cask-fonts/font-go-mono-nerd-font
homebrew/cask-fonts/font-go-mono-nerd-font-mono
homebrew/cask/go-agent
homebrew/cask/go-server

选择一个自己想要的版本,这里本人选择的是go@1.10版本。

安装brew下go@1.10版本:

louxiujun@IT-C02Z9057LVDL  $ brew install go@1.10
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.10/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.10/1.9.7: 7,668 files, 294.2MB

确认一下go@1.10的安装目录:

louxiujun@IT-C02Z9057LVDL  $ cd /usr/local/opt/
louxiujun@IT-C02Z9057LVDL  $ ls 
gettext      go@1.10      libidn2      libunistring openssl@1.1  wget

可以看到二进制的go@1.10已经被安装在该目录下了。

配置golang的相关环境变量

louxiujun@IT-C02Z9057LVDL  $ vim ~/.bashrc

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

#GOROOT
export GOROOT=/usr/local/opt/go@1.10

#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/java用于存放java代码等。

让改动立即生效:

louxiujun@IT-C02Z9057LVDL  $ source ~/.bashrc

试一试golang是否安装成功
出现以下内容,则安装成功

louxiujun@IT-C02Z9057LVDL  /usr/local/opt  go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/louxiujun/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/louxiujun/Documents/code/gopath"
GORACE=""
GOROOT="/usr/local/opt/go@1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go@1.10/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
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"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2h/43trw35142g1h7qs8zdgxbn40000gp/T/go-build985605356=/tmp/go-build -gno-record-gcc-switches -fno-common"
版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/3e2b4f80df88
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-09 22:17:53
  • 阅读 ( 1200 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢