1. Download the installation package
The download address of the installation package is: https://golang.org/dl/
If you can't open it, you can use this address: Downloads - The Go Programming Language
Note: Choose different versions according to the cpu, M series (ARM64)|Intel (x86-64)
After the download is complete, open the installation package and the next step will be OK
After the installation is complete, verify that the installation was successful
go version
The above prompt appears to indicate that the installation is ok.
2. Configure environment variables
1. Terminal input vim ~/.zshrc to enter the configuration file , input i to edit
2. Regardless of whether it is an empty text or a content, copy the code below at the end
*Note: GOPATH specifies the path to be modified to your own project path
#注意=号前后不能有空格
export GOPATH=/Users/chenshiwei/workspace/golang #请改成你的项目路径
export GOBIN=$GOPATH/bin #GOLANG运行目录一般不可修改
export PATH=$PATH:$GOBIN
3. After the paste is complete, enter esc at the bottom of the terminal : wq to save the modification and exit the editor
4. Enter source ~/.zshrc in the terminal to refresh the golang configuration file
5. Enter go env to check whether the configuration takes effect
setup complete
3. Open VScode to write the first piece of golang code
1. Enter the project root directory to create bin, pkg, src three directories
Describe the role of the three directories:
bin: store compiled binary files
pkg: store compiled library files
src: store your own code/package
2. Install the go plugin in vs code
After the installation is complete, we start to create our first go code. In the process of writing code, the lower right corner of vs code will prompt us to install some library files. We click Install All, but during the installation process, we find that the installation has been unsuccessful, and the following code appears:
leyangjundeMacBook-Pro:photoleyangjun$ go get -u github.com/spf13/cobra
go get -u github.com/spf13/cobrago get github.com/spf13/cobra: module github.com/spf13/cobra: Get https://proxy.golang.org/github.com/spf13/cobra/@v/list: dial tcp 142.251.10.141:443: i/o timeout
Reason: https://proxy.golang.org/github.com/ cannot be accessed in China
Solution: Change to a domestically accessible address and set the GOPROXY proxy
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=off #关闭包的有效性验证
go env -w GOSUMDB=sum.golang.google.cn #也可设置国内提供的sum 验证服务
Note: The -w flag requires one or more arguments of the form NAME=VALUE and overrides the default setting
After the setting is complete, we re-Install All and found that all the installations were successful this time.
3. Create our project directory under src, and create a main.go file under the directory with the following content:
package main
import "fmt"
func main() {
fmt.Println("hello world!!!")
}
4. The vs code terminal enters our current folder, enter go run main.go to execute our code
So far our environment configuration and the first golang program are completed, congratulations.
Thanks for reading, please leave a message if you have any questions.