This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Environment set up

First go to the go official website to download the GO installation package, according to their own operating system, select the corresponding installation package to download

This article uses macOS as an example to install the GO environment

After downloading the installation package, double-click the installation package to install the macOS in a simpleinstall mode. The default installation path of macOS go is:

The default installation path of Go on macOS is /usr/local/go. The default installation path of Go on Windows is C:/goCopy the code

After the installation is complete, configure the GO environment variables. Edit the.bash_profile file and add the following content to the file (according to your actual situation)

1, open the.bash_profile file vim ~/.bash_profile. 2, add the following to the configuration file (save and exit after editing # GOROOT = / usr/local/go go the installation PATH of the export PATH = $GOROOT/bin: $PATH export GOPATH = / Users/shulv/studySpace/GolangProject # our workspace. Behind will detail the export GOBIN = / Users/shulv/studySpace/GolangProject/bin# stored here is that the generated source code compiled executable file 3, just edit. Following immediately. Perform source ~ /. FollowingCopy the code

Run the following command to verify the installation

1. View the version information. Go Version 2Copy the code

If the output is correct, the environment is successfully installed

Introduction to Environment Variables

2. View the name of the specified GO env environment variable. For example, go env GOROOT view the go installation directory.Copy the code

Below are my local GO environment variables

GO111MODULE="" GOARCH="amd64" GOBIN="/Users/shulv/studySpace/GolangProject/bin" GOCACHE="/Users/shulv/Library/Caches/go-build" GOENV="/Users/shulv/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/shulv/studySpace/GolangProject/pkg/mod"  GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/shulv/studySpace/GolangProject" GOPRIVATE="" GOPROXY="<https://proxy.golang.org>,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR = "/ usr/local/go/PKG/tool/darwin_amd64" GOVCS = "" GOVERSION =" go1.16.6 GCCGO "=" GCCGO AR = "AR" CC "=" clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g  -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v2/8n4xnrln7l34q18v_2dt6tdr0000gn/T/go-build1451458835=/tmp/go-build -gno-record-gcc-switches -fno-common"Copy the code

There are many environment variables that can be set in GO, but many of them may not be used

GOROOT
GOPATH
GOBIN
GOOS
GOARCH
GOPROXY
GO111MODULE
Copy the code

GOROOT

GOROOT is the go installation directory. It is used to index the resources in the GO installation directory. For example, the bin directory in the Go installation directory contains the tools provided by go, such as some common GO commands. When the environment variable GOBIN is configured during the go environment installation, the value of the environment variable is GOROOT/bin, which is the bin directory under the go installation directory. The path is spliced into the GOROOT/bin directory, which is the bin directory under the GO installation directory. The path is spliced into the GOROOT/bin directory, which is the bin directory under the GO installation directory. And concatenated that PATH into the PATH so that you can use some of the commands that go provides globally

Directory structure in the go installation directory (the default MacOS go installation directory is /usr/local/go)

AUTHORS official Go language writer list. Md CONTRIBUTORS Third-party CONTRIBUTORS To the LICENSE Go language release LICENSE agreement PATENTS README. Md SECURITY Bin stores the auxiliary files of the Go API inspector. Bin stores the executable files of all the official Go language related tools. By default, this directory contains the go and Gofmt tools. Doc stores almost all of the go official documents and instructions in HTML, making it easy for developers to view the favicon.ico lib documentation template, misc stores plug-ins for various editors and IDE software. Assist them in viewing and writing Go code. The PKG is used to hold all the archive files of the Go language standard library after the build installation. The Go source file corresponds to the archive file ending in ". A ", which is stored in the platform related directory in the PKG folder. TXT Search engine robots file SRC store all the standard libraries, Go language tools, And related low-level libraries (C language implementation) source test to store the test Go language itself code filesCopy the code

GOPATH

Note: The GOPATH value cannot be the same as the GOROOT value

GOPATH sets up our development zone to store the source code for our project. On macOS or Linux, the default path for GOPATH is $HOME/go

GOPATH can set multiple values, that is, multiple workspaces, separated by semicolons

GOPATH=/Users/shulv/studySpace/GolangProject; /Users/shulv/studySpace/GolangProject1Copy the code

If GOPATH has two workspaces set up, when we run the go get command to get the remote library, it will select the first workspace to install

According to the go language development specifications, the workspace directory is generally divided into three subdirectories: SRC, PKG, bin

Bin: stores the compiled executable file.

PKG: stores the compiled package files.

SRC: Stores the project source files

You do not need to create the bin and PKG directories. The go command automatically creates the directory (for example, go install). You only need to manually create the SRC directory

GOBIN

GOBIN is used to store the binaries (executable files) generated by the compilation of our project code. When we use the go install command to compile and package our project code, it puts the resulting binaries into the directory specified by GOBIN. If the GOBIN directory is not specified, the default is GOPATH/bin

GOOS

The value of GOOS is the operating system for which the code is compiled. For example, Linux, Darwin, Windows, NetBSD, FreeBSD, OpenBSD, and Solaris

For macOS, the value is Darwin. To view this, run go env GOOS

GOARCH

The value of GOARCH is the CPU architecture or processor for which the code is compiled. Such as AMD64, 386, ARM and so on

GOARCH and GOOS come in pairs. For example, on macOS, they can come in several combinations

GOOS        GOARCH
darwin	    386
darwin	    amd64
darwin	    arm
darwin	    arm64
Copy the code

What do the GOOS and GOARCH environment variables do?

We need these two environment variables if we want to be able to generate programs on one platform that run on the other. For other programming languages, third-party tools are usually needed to create programs that can run on one platform on another, such as binary programs that can run on 64-bit Linux in a 32-bit Windows environment. In the GO language, however, you only need to set the GOOS and GOARCH environment variables

Let’s say I’m in Windows 32 and I want to build a target program that runs on Linux 64

Set GOOS and GOARCH to Linux AMD64Copy the code

GOPROXY

GOPROXY sets the list of proxy addresses that go Get uses to download dependencies. The environment variable can also be set to multiple values separated by commas or bars. The default value of GOPROXY is proxy.golang.org,direct

When the go command is used to look for dependent modules, it accesses each proxy in the GOPROXY list in order until it receives a successful response or a terminal error occurs

There may be two keywords in GOPROXY to replace the proxy URL

Off: Downloading dependent modules from any source is not allowed direct: Downloading directly from the version control repository, rather than using the module agentCopy the code

GO111MODULE

If you are a go language introduction small white, do not know what is the code package, command source file, etc., it is recommended to look at this part first, directly look at the final result, to solve the go installed, can not compile and run the command source file introduced other dependency package problem

GO111MODULE is a switch on go Modules, a dependency solution for the GO language, released in Go1.11 and recommended for production use in Go1.14. Below is a brief introduction to why the Go modules appear, more detailed content, will be in the following article system combing

To understand the following, we first need to know, when we compile a GO source file that depends on another package, what is go’s process for finding dependencies? This relates to the go version and the configuration of the GO111MODULE environment variable. My installed go version and GO111MODULE configuration are as follows:

Go version go1.16.6 Darwin /amd64 GO111MODULE configuration value (go env GO111MODULE command to view) GO111MODULE="" (I did not set this value, It defaults to an empty string.)Copy the code

Take a look at the directory structure of my project and the contents of the files

├─ bin ├─ PKG ├─ SRC ├─ entry │ ├─ study. Go ├─ studyGo ├─ learnGo Import "StudyGo "// Import the studyGo package func main() {studyGo.test () // use the Test() method under the studyGo package} learnGo. Go content package StudyGo import "fmt" func Test() { fmt.Println("Just a test") }Copy the code

Now I use go run study.go to compile and run the study.go source file.

Execution: go run study. Go output: study. Go: : : package studygo is not in GOROOT (/ usr/local/go/SRC/studygo)Copy the code

Note: The studyGo package is not in the GOROOT path. That is, it will look for dependencies in the GOROOT directory during compilation. How does it find this package, which is our environment variable GO111MODULE

GO111MODULE sets different values, what is the effect

To understand what problems go Modules solves, you need to know what happens when I set different values

  • GO111MODULE=off, no module is supported. The go command line will not support the Module function, and dependencies will be found through vendor or GOPATH mode as in older versions. This means that when we compile and execute a GO source file, if the file has dependencies on other packages, it will first look in the GOROOT path and then in the GOPATH path
  • GO111MODULE= ON, module support. The go command line uses modules and doesn’t look in the GOPATH directory at all
  • GO111MODULE=auto, default value. The go command line determines whether to enable module based on the current directory. This can be divided into two cases: (1) the current directory is outside GOPATH/ SRC and the directory contains the go.mod file, enabling module support. (2) The current file is in the directory containing the go.mod file.

The GO111MODULE is null by default. When the value is auto, go does not look for dependencies in GOPATH, so when we compile and run study. Go, we will tell you that no dependencies are found. The solution is to set GO111MODULE to off. The expectation is that when it looks for dependencies, it will look under GOPATH if it does not find them under GOROOT

Set GO111MODULE to off go env-w GO111MODULE=offCopy the code

Then compile and execute study.go, and you can print out the results normally

What problem does Go Modules solve?

  1. Solve the Go language’s long-standing dependency management problem
  2. “Phasing out” existing GOPATH usage patterns
  3. Other dependency management tools in the Unified community (providing migration capabilities)

Why eliminate GOPATH usage patterns? What are the disadvantages of the GOPATH usage pattern? How to manage a new project with the Go Mod? These will be shared in more detail in the next article

This is just to help you understand why we have problems compiling and running a source file with dependencies after installing GO using the default configuration. There are a lot of content about Go Modules, here because of the length of the reason, not here in detail, will be in the following to find a separate article to share. The above content, if there is an incorrect understanding of the place, welcome everyone to comment area to correct

Thanks for the following excellent articles

Golang environment variable Settings detailed

Golang Environment variables

Go Starter Guide

Go language: set the environment variables GOPROXY and GO111MODULE

Go Modules The ultimate introduction