一、通過Homebrew安裝Golang,配置Golang
安裝Homebrew
Homebrew官網(wǎng)安裝方法:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝Golang
brew install go
為Golang配置環(huán)境變量
創(chuàng)建目錄
在文稿(Documents)目錄下穿件一個(gè)目錄(go)作為GOPATH,在GOPATH的目錄下新建src、bin兩個(gè)目錄了。這兩個(gè)目錄的用途如下:
1、 src:存放源代碼;
2、 bin:存放編譯后生成的可執(zhí)行文件;
配置全局變量
1、打開終端輸入cd ~進(jìn)入用戶主目錄;
2、輸入ls -all命令查看是否存在.bash_profile;
3、存在既使用vim .bash_profile 打開文件;
4、輸入 i 進(jìn)入vim編輯模式;
5、輸入下面代碼, 其中 GOPATH: 日常開發(fā)的根目錄。GOBIN:是GOPATH下的bin目錄。
export GOPATH=/Users/自己的用戶目錄(lwtz)/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
6、點(diǎn)擊ESC,并輸入 :wq 保存并退出編輯??奢斎雟im .bash_profile 查看是否保存成功;
7、輸入source ~/.bash_profile 完成對golang環(huán)境變量的配置,配置成功沒有提示。
8、輸入go env 查看配置結(jié)果:
GOARCH="amd64"
GOBIN="/Users/lwtz/Documents/go/bin"
GOCACHE="/Users/lwtz/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/lwtz/Documents/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.2/libexec/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/sc/kxtrgn491415m5qjj5ly5s_m0000gn/T/go-build011763803=/tmp/go-build -gno-record-gcc-switches -fno-common"
二、搭建開發(fā)工具
到Atom官網(wǎng)下載Atom macOS的安裝包并安裝
安裝go-plus
在Atom中的Preference中可以找到install菜單,輸入go-plus,點(diǎn)擊Packages

點(diǎn)擊:install,就會(huì)開始安裝go-plus,我的已經(jīng)安裝過了就變成Uninstall了。
安裝atom-terminal-panel插件
go-plus沒有提供編譯工具,可以在命令行中直接運(yùn)行g(shù)o程序,需要安裝atom-terminal-panel,在install中直接輸入atom-terminal-panel安裝。

直接使用快捷鍵control+`就可以呼出terminal。
當(dāng)然也可以在macOS系統(tǒng)的終端上編譯
三、第一個(gè)程序:HelloWorld
在src目錄下新建helloworld目錄,類似于一個(gè)project的名稱吧,在helloworld目錄下新建helloworld.go文件,在文件中輸入下面的代碼:
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
在終端切換到helloworld目錄,執(zhí)行命令行go run helloworld.go輸出
192:~ lwtz$ cd /Users/lwtz/Documents/go/src/helloworld
192:helloworld lwtz$ go run helloworld.go
Hello world
192:helloworld lwtz$