In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to configure the Golang environment in the Mac environment". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to configure the Golang environment in the Mac environment.
Configure Golang environment (Mac, vscode, domestic)
Download Golang
Because of the existence of Homebrew, it is very convenient to download anything on Mac. You can install Homebrew by running the following command:
/ usr/bin/ruby-e "$(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
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 the Go language:
$brew install go
After the installation is complete, you can run the following command to test it:
$go version
Set $GOPATH
The Go language requires you to provide a $GOPATH variable in the environment variables of the system, which, as the name implies, provides a folder location for the Go language to manipulate.
We can set environment variables in two ways
First kind
Set the variable directly in the ~ / .bash_profile file. The specific operations are as follows:
$sudo nano ~ / .bash_profile
Running the above command will open a nano editor on 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 at will. When the input is complete, press ctrl + o and then press enter to save. Finally, press ctrl + x to exit the nano editor.
The second kind
It is too troublesome to modify environment variables every time through sudo nano ~ / .bash_profile, and it is also troublesome to modify .bash _ profile through other editors, such as vscode. So is there a simpler way?
We can create another file to hold the environment variables. For example, if we create a file $HOME/Developer/index.sh, we can enter the original export GOPATH=$HOME/Developer/go into this file. At this point, we use the sudo nano ~ /. Bash_profile command to delete the export GOPATH=$HOME/Developer/go from the original .bash_profile file, add the line source $HOME/Developer/index.sh, and save the exit. In the future, you can directly modify the $HOME/Developer/index.sh to set the environment variables instead of modifying the ~ / .bash_profile file. The principle of this is that the source command will introduce the contents of ~ / Developer/index.sh.
Configure $PATH
When you have finished configuring $GOPATH, you also need to configure $PATH. This is because sometimes we have to run some Golang binaries directly on the terminal, and if you don't include the folder where Golang binaries are stored in $PATH, the terminal won't find them. There are usually two folders for Golang binaries. The first is $GOPATH/bin, and the second is $GOROOT/bin. You may wonder what the $GOROOT here is. In fact, it is the location where the Golang source code is stored, and there are some library files that come with Golang. We don't need to set $GOROOT on Mac, but we need it on Windows. In order to make it easier to understand, we can also set it here. If you install Golang using Homebrew, $GOROOT will be mapped to / usr/local/opt/go/libexec. So using the second way we set $GOPATH above, add this line to the index.sh file to set GOROOT: export GOROOT=/usr/local/opt/go/libexec. We can also set $PATH on index.sh. To simplify, I'll show you the full index.sh directly, as follows:
Export $GOPATH=$HOME/Developer/goexport $GOROOT=/usr/local/opt/go/libexecexport PATH=$PATH:$GOPATH/bin:$GOROOT/bin
Configure Visual Studio Code
The first reason I love vscode is its light weight, and the second reason is its versatility. It's really lightweight. Anyway, I haven't encountered stutter on my macbook pro 2013 (8g i7). If I use goland, I will often get stuttered. Of course, if your computer is configured badly (such as iMac Pro), you can certainly ignore this. The comprehensiveness is that it has a powerful community with feature-rich plug-ins on which you can program almost any language. Needless to say, let's look at how to configure the Go locale above.
Download the official Golang plug-in
Vscode downloads plug-ins very well, select Extensions in the leftmost vertical navigation bar. Then the first plug-in to search go in the search box is the official (Microsoft) Go language plug-in, which can be downloaded.
If you are interested, you can visit the website of the official Golang plugin: https://github.com/microsoft/vscode-go
Install the Golang official plug-in dependency package
After you download the plug-in, every time you open a golang file (.go), it will remind you to install some dependent packages (which are actually packages written in Golang). You can install all installation packages by clicking the install all option on the right side of the reminder box. But after running for a while, you will find that many packages have failed to install:
Installing github.com/mdempsky/gocode FAILEDInstalling github.com/ramya-rao-a/go-outline FAILEDInstalling github.com/acroca/go-symbols FAILEDInstalling golang.org/x/tools/cmd/guru FAILEDInstalling golang.org/x/tools/cmd/gorename FAILEDInstalling github.com/stamblerre/gocode FAILEDInstalling github.com/ianthehat/godef FAILEDInstalling github.com/sqs/goreturns FAILEDInstalling golang.org/x/lint/golint FAILED9 tools failed to install.
The reason is that due to some well-known reasons, golang.org cannot be accessed in China, so it is naturally impossible to download the resources under it. At this point, we can set $GOPROXY to solve this problem. Setting up $GOPROXY is actually setting up a proxy to help you access and install these packages, rather than through your own network. The agent I personally use is this: export GOPROXY= "https://athens.azurefd.net". Similarly, you can write this line of code into the index.sh file, and the updated index.sh file looks like this:
Export $GOPATH=$HOME/Developer/goexport $GOROOT=/usr/local/opt/go/libexecexport PATH=$PATH:$GOPATH/bin:$GOROOT/binexport GOPROXY= "https://athens.azurefd.net"
Here are the other agents available:
Export GOPROXY= "https://goproxy.io"export GOPROXY=" https://goproxyus.herokuapp.com"export GOPROXY= "https://goproxy.cn"# the latest official export GOPROXY=" https://proxy.golang.org"
At this point, you can install these packages by opening another Golang file and popping up a reminder box. Or install these dependency packages directly by pressing cmd+shift+p on vscode to pop up the vscode command box, and then typing > Go: Install/Update Tools.
In fact, vscode installs these packages through the go get command, which installs the source code to $GOPATH/src and the binaries of the corresponding package to $GOPATH/bin. When you complete the installation, you will find a lot of binaries when you go to $GOPATH/bin. The official Golang plug-in helps you optimize your programming experience by automatically finding and using these binaries. For example, gocode helps to complete the code automatically.
Thank you for reading, the above is the content of "how to configure the Golang environment in the Mac environment". After the study of this article, I believe you have a deeper understanding of how to configure the Golang environment in the Mac environment, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.