Steps to set up the go language development environment in Mac

  • 2020-06-03 06:49:33
  • OfStack


Go is a new statically typed development language and has many exciting new features compared to current development languages. Optimized for application programming on multiprocessor systems, the go language can match the speed of c and c++, and is more secure, concise, and supports parallel processes.

Here are the main features of the go language:

1. Automatic garbage collection

2. Richer built-in types

3, function multiple inverse value

4. Error handling

Anonymous functions and closures

6. Types and interfaces

7. Concurrent programming

8 and reflection

9. Language Interaction

Mac development environment setup

All of the above is nonsense, building development environment as soon as possible to start the journey of programming is the king, the author USES the mac system, so the golang environment under mac is introduced.

1. Install SDK for Golang

google website by walls download address is as follows: http: / / www golangtc. com/download, download the latest installation package, then double-click the installation.
After the installation is completed, open the terminal, enter go or go version(view the installation version), and the following information will indicate that the installation is successful:

LCore:~ lcore$ go version go version go1.4.1 darwin/amd64 LCore:~ lcore$

2. Configure environment variables

After installing sdk, the next step is to configure environment variables and open terminal input cd ~ Enter the user home directory and enter ls -all The command checks to see if it exists .bash_profile The file exists both for use vim .bash_profile Open and edit the file. If it doesn't exist, create 1. According to my actual situation, the contents are as follows:

export GOPATH=/Users/lcore/dev/code/go
export GOBIN=$GOPATH/bin

GOPATH: The root directory for daily development.

GOBIN: Is the bin directory under GOPATH.

And the gobin directory needs to be added to the path path, the generated executable file can be run directly.

Exit vim and use source ~/.bash_profile You are ready to configure the golang environment variable and enter it in the terminal go env View the configured effect:

LCore:~ lcore$ go env GOARCH="amd64" GOBIN="/Users/lcore/dev/code/go/bin" GOCHAR="6" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/lcore/dev/code/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"

3. Development tool configuration (sublime text)

Here I choose is sublime text The installation gosublime The plug-in is developed (golang syntax highlighted), and the installation method is not mentioned here.

Hello GO

After the development environment is configured, it is easy to develop go language for 1 time. In your gopath Under the src Under the directory, create a new folder (named after the project name), and then in ls -all0 Open the folder and create a new one main.go The file is now ready to be encoded.

package main

import ( "fmt" ) func main() { fmt.Println("hello go"); }

After the code is written, use command+b Open the sublime text Terminal, in use go build xx (project name) is compiled and the resulting information is as follows:

[ `go build helloGo` | done: 420.495985ms ]

When prompted for successful compilation, execute the shell command to execute the file just compiled ./hellogo You can see the operation results:

[ `./helloGo` | done: 10.532868ms ]
 hello go

If you simply need to see the result of the run without producing an executable file (file name and project name 1), then you can use the sublime text Directly used in the terminal go run xxx.go You can:

[ `go run main.go` | done: 314.476988ms ]
 hello go


So far, we've installed the basic environment for developing the golang program, and we're ready to enjoy the wonders of golang! I hope this article can be helpful to the Go development environment. If you have any questions, please leave a message.

Related articles: