As a cross-platform programming language, Golang (also known as Go) is very suitable for developing web services, network applications, distributed systems, etc. This article will introduce you to how to install and set up Golang on MacOS.
1. Download and install Golang
You can download the latest version of Golang from the official website https://golang.org/dl/. After visiting the page, you can find the macOS installation package under the "Downloads" column.
The file name of the installation package is usually "go version_number.darwin-amd64.pkg", "version_number" refers to the version number of the installation package. This installation package only supports Mac OS X 10.8 (Mountain Lion) or higher. If your Mac operating system is older than Mountain Lion, please download the older version of Golang suitable for you from the official website.
After downloading the installation package, double-click it and install Golang through the installer. Follow the on-screen prompts to complete the installation.
2. Set environment variables
After installing Golang, we need to add the path of Golang to the environment variable. In this way, we can use Golang’s command line tools.
To add environment variables, open a terminal and enter the following commands:
$ echo "export GOPATH=$HOME/go" >> ~/.bash_profile $ echo "export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin" >> ~/.bash_profile $ source ~/.bash_profile
These commands will add GOPATH and PATH to your bash profile file. GOPATH is the directory where your Golang project is stored, and PATH is the search path for running Golang commands.
3. Test installation
To test whether you have successfully installed Golang, please open the terminal and enter the following command:
$ go version
If you see the following output, it means you Golang has been successfully installed:
go version go1.16 darwin/amd64
4. Installation support tools
Golang provides us with some very useful support tools, such as GoDoc and GoFmt, which can help us better manage and Write Golang code. To install these support tools, run the following commands:
$ go get golang.org/x/tools/cmd/godoc $ go get golang.org/x/tools/cmd/goimports $ go get golang.org/x/tools/cmd/gorename $ go get github.com/golang/lint/golint
These commands will use Golang's own toolkit to download and install these support tools into your GOPATH/bin directory. Once installed, you can run these tools to use their functionality.
Summary
Through the above steps, you have successfully installed Golang, set environment variables, and installed support tools. Now you can start writing your web services, network applications, distributed systems and more with Golang!
The above is the detailed content of How to install and set up Golang on MacOS. For more information, please follow other related articles on the PHP Chinese website!