SoFunction
Updated on 2025-03-05

Mac OS installation golang tutorial

Download the golang installation package

Download address:

/download
/p/go/downloads/list
go1.-amd64-osx10.  go1.4 Mac OS X (x86 64-bit) PKG installer

Set environment variables

Configure GOROOT and GOPATH:

Copy the codeThe code is as follows:

Create a go folder in the directory: mkdir ~/go

Put the following in .bash_rc (or maybe .bash_profile or .zshrc)
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin

Download pkg and double-click to install it under mac. It is super simple. The default installation path: /usr/local/go

Copy the codeThe code is as follows:

source ~/.bash_profile

At this point, the installation of golang is completed, let’s experience it:

Copy the codeThe code is as follows:

 go env
 go version

Running results:
Copy the codeThe code is as follows:

liuxinmingdeMacBook-Pro:gotest liuxinming$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/liuxinming/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
============== Gorgeous dividing line====================
liuxinmingdeMacBook-Pro:gotest liuxinming$ go version
go version go1.4 darwin/amd64
liuxinmingdeMacBook-Pro:gotest liuxinming$

Development Tool Configuration (Sublime Text 2)

Sublime Text 2 Download address:/2 

After downloading, install it directly.

Sublime Text 2 can be used for free, but after the number of savings reaches a certain number, it will prompt you to purchase it. Click Cancel to continue using it. There is no difference between it and the official registered version.

Start your golang journey

Command + Shift + P Open Package Control, then enter Go and Enter (this will cause a code prompt)

Create a new one

Copy the codeThe code is as follows:

package main

import "fmt"

func main() {
    ("hello, world\n")
}

For the written file, use the shortcut key Command + B to open the terminal of Sublime Text 2 and enter go build (name) to compile it: [Note: I tested it, and the shortcut key will be automatically compiled without any input]

The problem is here, the default installation of Sublime does not have go build, so you need to create one by yourself.

Adding a Go Build for Sublime Text2

Tools -> Build System -> New Build System

Enter the following code:

Copy the codeThe code is as follows:


     "cmd" : [ "/usr/local/go/bin/go" ,  "run" ,  "$file" ], 
     "file_regex" :  "^(...*?):([0-9]*):?([0-9]*)" , 
     "working_dir" :  "${file_path}" , 
     "selector" :  ""  

Then select the Build file: go

Then Command + B has the output.