search
HomeBackend DevelopmentGolangHow to configure the Golang environment on Mac

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)"

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

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

$ go version

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

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

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.

原因是因为一些众所周知的原因,在国内无法访问 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"

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

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

这时,你可以通过再打开一个 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!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Learn Go String Manipulation: Working with the 'strings' PackageLearn Go String Manipulation: Working with the 'strings' PackageMay 09, 2025 am 12:07 AM

Go's "strings" package provides rich features to make string operation efficient and simple. 1) Use strings.Contains() to check substrings. 2) strings.Split() can be used to parse data, but it should be used with caution to avoid performance problems. 3) strings.Join() is suitable for formatting strings, but for small datasets, looping = is more efficient. 4) For large strings, it is more efficient to build strings using strings.Builder.

Go: String Manipulation with the Standard 'strings' PackageGo: String Manipulation with the Standard 'strings' PackageMay 09, 2025 am 12:07 AM

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

Mastering Byte Slice Manipulation with Go's 'bytes' Package: A Practical GuideMastering Byte Slice Manipulation with Go's 'bytes' Package: A Practical GuideMay 09, 2025 am 12:02 AM

ThebytespackageinGoisessentialforefficientbyteslicemanipulation,offeringfunctionslikeContains,Index,andReplaceforsearchingandmodifyingbinarydata.Itenhancesperformanceandcodereadability,makingitavitaltoolforhandlingbinarydata,networkprotocols,andfileI

Learn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageLearn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageMay 08, 2025 am 12:13 AM

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

Go: Byte Slice Manipulation with the Standard 'bytes' PackageGo: Byte Slice Manipulation with the Standard 'bytes' PackageMay 08, 2025 am 12:09 AM

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Go encoding/binary package: Optimizing performance for binary operationsGo encoding/binary package: Optimizing performance for binary operationsMay 08, 2025 am 12:06 AM

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go bytes package: short reference and tipsGo bytes package: short reference and tipsMay 08, 2025 am 12:05 AM

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

Go bytes package: practical examples for byte slice manipulationGo bytes package: practical examples for byte slice manipulationMay 08, 2025 am 12:01 AM

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software