How to configure the Golang environment on Mac

藏色散人
Release: 2021-11-03 17:17:50
forward
4952 people have browsed it

This article is provided by the go language tutorial column to introduce how to configure the Golang environment in the Mac environment. I hope it will be helpful to friends in need!

Configure Golang environment (Mac, vscode, domestic)

Download Golang

Because of the existence of Homebrew, on Mac It is very convenient to download anything. You can run the following command to install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy after login

For more information about Homebrew, you can visit their website: brew.sh/

After the installation is complete, you can use the following command to install Go Language:

$ brew install go
Copy after login

After the installation is complete, you can run the following command to test it:

$ go version
Copy after login

Set $GOPATH

Go language requires you to be in the system A $GOPATH variable is provided in the environment variables. As the name suggests, it provides the Go language with a folder location for it to operate.

We can set environment variables in the following two ways

The first one

Set the variables directly in the ~/.bash_profile file. The specific operations are as follows:

$ sudo nano ~/.bash_profile
Copy after login

Running the above command will open a nano editor in the terminal to edit the ~/.bash_profile file. You can add a line to the .bash_profile file: export GOPATH=$HOME/Developer/go

$HOME/Developer/go is my favorite GOPATH folder location, you can set a folder location arbitrarily. After completing the input, press ctrl o and then press enter to save. Finally press ctrl x to exit the nano editor.

The second type

If it is too troublesome to modify environment variables through sudo nano ~/.bash_profile every time, and it is also necessary to modify .bash_profile through other editors, such as vscode every time Password authorization is also very troublesome. So is there a simpler way?

We can create another file to store environment variables. For example, if we create a file $HOME/Developer/index.sh, we can input the original export GOPATH=$HOME/Developer/go into this file. At this time, we use the sudo nano ~/.bash_profile command to delete export GOPATH=$HOME/Developer/go in the original .bash_profile file, and add this line source $HOME/Developer/index.sh, then save and exit . In this way, you can directly modify $HOME/Developer/index.sh to set environment variables instead of modifying the ~/.bash_profile file. The reason for this is that the source command will import the contents of ~/Developer/index.sh.

Configure $PATH

After you configure $GOPATH, you also need to configure $PATH. This is because sometimes we need to run some Golang binary files directly in the terminal. If you do not include the folder storing Golang binaries in $PATH, the terminal cannot find them. There are generally two folders that store Golang binary files. The first is $GOPATH/bin, and the second is $GOROOT/bin. You may be wondering what $GOROOT is here. In fact, it is the location where Golang source code is stored, which contains some of Golang's own library files. We don't need to set $GOROOT on Mac, but it is required on Windows. For ease of understanding, we can also set it here. If you use Homebrew to install Golang, $GOROOT will be mapped to /usr/local/opt/go/libexec. Then using the second method of setting $GOPATH above, add this line to the index.sh file to set GOROOT: export GOROOT=/usr/local/opt/go/libexec. At the same time, we can also set $PATH on index.sh. In order to simplify the explanation, I will directly show you the complete index.sh as follows:

export $GOPATH=$HOME/Developer/go
export $GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin:$GOROOT/bin
Copy after login

Configuring Visual Studio Code

The first reason I love vscode is that it The second is its lightweight and versatility. It is really lightweight. Anyway, I have not encountered any lag when using it on my macbook pro 2013 (8g i7). If I use goland, I will often lag. Of course, if your computer configuration is incredible (such as iMac Pro), you can certainly ignore this. Its comprehensiveness lies in the fact that it has a strong community with feature-rich plug-ins, and you can program almost any language on it. Without further ado, let’s take a look at how to configure the Go locale above.

Download the official Golang plug-in

It is very convenient to download the plug-in on Vscode. Select Extensions in the vertical navigation bar on the far left. Then search for go in the search box. The first plug-in is the official (Microsoft) Go language plug-in. Just download it.

If you are interested, you can visit the official Golang plug-in URL: https://github.com/microsoft/vscode-go

Install Golang official plug-in dependency package

After you download this plug-in, every time you open a golang file (.go), it will remind you to install some dependency packages (in fact, these dependency packages are packages written in Golang). You can click the install all option on the right side of the reminder box to install all installation packages. But after running for a period of time, you will find that many packages have failed to install:

Installing github.com/mdempsky/gocode FAILED
Installing github.com/ramya-rao-a/go-outline FAILED
Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing golang.org/x/tools/cmd/gorename FAILED
Installing github.com/stamblerre/gocode FAILED
Installing github.com/ianthehat/godef FAILED
Installing github.com/sqs/goreturns FAILED
Installing golang.org/x/lint/golint FAILED
9 tools failed to install.
Copy after login

原因是因为一些众所周知的原因,在国内无法访问 golang.org,自然也就无法下载在其下的资源。这时我们可以设置 $GOPROXY来解决这个问题。设置$GOPROXY 其实就是设置一个代理帮你去访问和安装这些包,而不是通过你自己的网络。我个人使用的代理是这个:export GOPROXY="https://athens.azurefd.net"。同样的,你可以把这行代码写进index.sh 文件,那么更新后的index.sh 文件就是这样的:

export $GOPATH=$HOME/Developer/go
export $GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin:$GOROOT/bin
export GOPROXY="https://athens.azurefd.net"
Copy after login

以下是现有的其它可用的代理:

export GOPROXY="https://goproxy.io"
export GOPROXY="https://goproxyus.herokuapp.com"
export GOPROXY="https://goproxy.cn"
# 最新官方的
export GOPROXY="https://proxy.golang.org"
Copy after login

这时,你可以通过再打开一个 Golang 文件弹出提醒框的方式来安装这些包。或者直接通过在 vscode 上按 cmd+shift+p 弹出 vscode 的命令框,然后输入 >Go: Install/Update Tools 来安装这些依赖包了。

其实 vscode 是通过 go get 命令来安装这些安装包的,go get 命令会把源代码安装到 $GOPATH/src, 同时把相应包的二进制文件安装到 $GOPATH/bin。 当你安装完成之后,你去到 $GOPATH/bin 会发现多了很多二进制文件。而官方 Golang 插件就是通过自动找到并使用这些二进制文件来帮你优化编程体验的。比如 gocode 是帮忙自动补全代码的。

自此,我们关于 Golang 的环境配置(在 Mac、vscode以及国内)就完成了。

The above is the detailed content of How to configure the Golang environment on Mac. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template