Mac Golang开发环境搭建

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

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


Mac Golang开发环境搭建_第1张图片
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
[email protected] ✔                                gst-plugins-good
[email protected]                                  gx-go
[email protected]                                  hugo
[email protected]                                   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

选择一个自己想要的版本,这里本人选择的是[email protected]版本。

安装brew下[email protected]版本:

louxiujun@IT-C02Z9057LVDL  $ brew install [email protected]
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/[email protected]_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring [email protected]_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/[email protected]/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/[email protected]/bin:$PATH"' >> ~/.bash_profile

==> Summary
  /usr/local/Cellar/[email protected]/1.9.7: 7,668 files, 294.2MB

确认一下[email protected]的安装目录:

louxiujun@IT-C02Z9057LVDL  $ cd /usr/local/opt/
louxiujun@IT-C02Z9057LVDL  $ ls 
gettext      [email protected]      libidn2      libunistring [email protected]  wget

可以看到二进制的[email protected]已经被安装在该目录下了。

配置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/[email protected]"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/[email protected]/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"

你可能感兴趣的:(Mac Golang开发环境搭建)