Mac Golang开发环境搭建

更多精彩内容Mac os安装golang开发环境夏之绘Go语言开发环境搭建详解飞雪无情flysnow_org有Go语言实战培训班吗?go语言开发环境搭建qfliweiminGo语言环境搭建详解Felixxingolang 新人入门配置学习molscar · · 711 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

由于需要进行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"

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:更多精彩内容Mac os安装golang开发环境夏之绘Go语言开发环境搭建详解飞雪无情flysnow_org有Go语言实战培训班吗?go语言开发环境搭建qfliweiminGo语言环境搭建详解Felixxingolang 新人入门配置学习molscar

查看原文:Mac Golang开发环境搭建

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

711 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传