• Golang Getting started (1) : Install and configure the meaning of environment variables
  • Golang Primer (2) : Learn the basic grammar of GO in one day
  • Golang Primer (3) : Learn advanced GO grammar in one day
  • Golang 101 (4) : Concurrency

Abstract

When learning Java a few years ago, the configuration of the environment discouraged some beginners. For Golang, it’s a good place to start. This article will start with how to install Golang, and then cover the configuration of GOROOT and GOPATH environment variables in Golang and what effect they have. Here we go:

The installation

Like other languages, golang can be installed in one of three ways, from simple to complex: by installing executable files, official binary packages, and source code compilations. The latest version of the GO installation file can be obtained here.

As an explanation, binaries are installed in the same way as executables, but installing from executables in Windows automatically configates the GOROOT environment variable, making it much easier.

This section uses Windows as an example. For details about how to install other operating systems, see the official documents.

For Windows users, there is a choice of MSI installer or ZIP package:

MSI installation

Open the downloaded MSI file and follow the prompts to install the Go tool. By default, the installer places the Go distribution in C:\Go.

This installer should place the C:\Go\bin directory in your PATH environment variable.

To complete.

ZIP file Installation

Download the ZIP file and unzip it (assuming it’s already unzipped into C:\Go) :

Add the bin subdirectory in your Go root (such as C:\Go\bin) to your PATH environment variable, as shown:

test

On the command line, enter

go version
Copy the code

You should see the version of Go you have installed, as shown below:

The environment variable

GOROOT

I don’t know if you’ve thought about why we need to configure environment variables.

An environment variable is an object with a specific name in the operating system that contains information that will be used by one or more applications. For example, the path environment variable in Windows and DOS operating systems, when the system is asked to run a program without telling it the full path of the program, the system should not only look for the program in the current directory, but also look for the path specified in the path. Users can set environment variables to better run the process.

This is the definition of environment variables in Baidu Encyclopedia, that is to say, environment variables are to facilitate us to better run a program, the program added to the system environment variables, will be directly executed on the command line, without the need for us to use the absolute path to execute. For example, in the process of using Golang, we often use go Get. Git clone can be used when using Git. For example, use GO:

If we need to see the version of go, we usually type go version on the command line, which in this case is all:

C:\Go\bin\go.exe version
Copy the code

In other words, the operating system creates a shortcut path for all programs that add environment variables to make user access easier and faster. Git XXX has been added to the environment variables in the system. Git XXX has been added to the environment variables in the system.

GOPATH

GOPATH is also known as Golang’s working directory. There are two types of GOPATH: global GOPATH and project-specific GOPATH.

In general, GOPATH is the target path of go commands, such as Go run, go install, and go get. The directory where these commands operate is GOPATH.

Let’s talk more about what structures GOPATH contains:

├─ bin ├─ PKG ├─ SRC. (GOPATH path) ├─ bin ├─ PKG ├─ SRCCopy the code
  • SRC stores our source code for our projects (e.g..go.c.h. s, etc.)
  • PKG Intermediate files generated at compile time (for example:.a)
  • Bin An executable file generated after compilation

In the Golang project, all the source code is placed in/SRC, the general practice is one project per directory.

$GOPATH/ SRC/XXX = $GOPATH/ SRC/XXX = $GOPATH/ SRC/XXX = $GOPATH/ SRC/XXX = $GOPATH/ SRC/XXX = $GOPATH/ SRC/XXX The corresponding effect of the go get command is to introduce remote packages to the selected project, which can be interpreted as introducing dependencies. For a project that has Package main, the go file is the main file and will be compiled and run.

Global GOPATH versus project GOPATH

However, we all know that when we develop Java projects, the project location and Maven Repository location are different. In the above setup, it was obvious that our project was written in the same place as the other packages we introduced, which was not elegant and could lead to some weird compile-time errors due to reference errors.

Therefore, we should set up a separate GOPATH for each project. This way, when a PROJECT references a package, it first looks in GOROOT/ SRC, such as the FMT input/output package. If it can’t find it, it looks for PROJECT GOPATH/ SRC. If it still can’t find it, it looks for GLOBAL GOPATH/ SRC. We can put some generic packages in the global directory and define the rest. It not only ensures the cleanliness of project files, but also ensures the correctness of compilation.

How do I set/modify GOPATH

For the global GOPATH, we can use the go env command to check the current global GOPATH:



D:\Go_path

platform GOPATH default values For example,
Windows %USERPROFILE%/go C:\Users\ user name \go
Unix $HOME/go /home/username /go

That is, you just need to set a directory where you want it, add it to the environment variable, and name it GOPATH. Then you can set the directory to GOPATH:

Note that some users may create folders and add environment variables, but not be able to modify GOPATH

This is because only the gopath folder is created, but this folder does not contain the SRC, PKG, bin folders, so go cannot recognize this folder as the Gopath path.

Write in the last

First of all, thank you for seeing here

The author is actually a Java developer, who has just started to contact Golang, a young language, and wants to better digest what he has learned through blog, on the one hand, and on the other hand, share his views with everyone, so that we can make progress together.

Of course, because the author knows little, there must be a lot of omissions or misunderstanding, if you find in this article with your understanding of the place, or where the author said more vague and not clear place, please do not hesitate to give advice and exchange (bow again!

Finally, thanks again ~

PS: If you have other questions, you can also find the author on the official account. And, all articles will be updated in the public account at the first time, welcome to find the author to play ~