Home  >  Article  >  Backend Development  >  Detailed explanation of GOROOT, GOPATH and GOBIN in Go language

Detailed explanation of GOROOT, GOPATH and GOBIN in Go language

尚
forward
2019-12-28 17:24:593255browse

Detailed explanation of GOROOT, GOPATH and GOBIN in Go language

Go language is a brand new static type development language, with automatic garbage collection, rich built-in types, multiple function return values, error handling, anonymous functions , Concurrent programming, reflection and other features.

The go command relies on an important environment variable: $GOPATH
GOPATH allows multiple directories. When there are multiple directories, please pay attention to the separator. When there are multiple directories, Windows uses a semicolon;

When there are multiple GOPATHs, the package obtained by go get will be stored in the first directory by default.

The $GOPATH directory is agreed to have three subdirectories

1 , src stores source code (for example: .go .c .h .s, etc.) According to golang's default convention, the current working path of go run, go install and other commands (that is, execute the above commands under this path).

2. The intermediate file generated when pkg is compiled (for example: .a) When golang compiles the package

3. The executable file generated after bin compilation (for convenience, you can add this directory to In the $PATH variable, if there are multiple gopaths, use ${GOPATH//://bin:}/bin to add all bin directories)

Code directory structure planning

The src directory under GOPATH is the main directory for the subsequent development of the program. All source codes are placed under this directory. So generally our approach is to have one directory and one project,

For example: $ GOPATH/src/mymath represents the mymath application package or executable application. This depends on whether the package is main or other. If it is main, it is an executable application. If it is other, it is an application package. This package will be introduced in detail later.

First take a look at my go environment: go env

C:\Users\Administrator>go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\project
set GORACE=
set GOROOT=D:\BaiduNetdiskDownload\go
set GOTOOLDIR=D:\BaiduNetdiskDownload\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

GOROOT

is actually the installation path of golang
After you install golang, this actually already exists

GOBIN

First look at the structure:

Detailed explanation of GOROOT, GOPATH and GOBIN in Go language

##We usually execute go build in the project directory, for example:

D:\project\src\go_dev\day1\package_example\main>go run main.go
400 100

Now we need to compile main.go, golang will automatically go to src to find the hello directory, because the code in my main.go is imported. Packag main package, so it can be compiled into an executable file, but by default the executable file is generated in the current directory. Although the directory can be specified, it still feels not very convenient

d:\project>go build go_dev/day1/package_example\main

So there are two other very good Commands used: go get and go install

go get

go get will do two things:

1. To download from a remote location, use Package

2. Execute go install

go install

go install will generate an executable file and place it directly in the bin directory. Of course, this There are prerequisites

What you compile is an executable file. If it is an ordinary package, it will be compiled and generated in the pkg directory. The file ends in .a

About go The entire development directory

go_project     // go_project为GOPATH目录
  -- bin
     -- myApp1  // 编译生成
     -- myApp2  // 编译生成
     -- myApp3  // 编译生成
  -- pkg
  -- src
     -- myApp1     // project1
        -- models
        -- controllers
        -- others
        -- main.go 
     -- myApp2     // project2
        -- models
        -- controllers
        -- others
        -- main.go 
     -- myApp3     // project3
        -- models
        -- controllers
        -- others
        -- main.go

Configuring the go environment under Linux

1. First download the go package under Linux: https://studygolang.com/ dl/golang/go1.9.2.linux-amd64.tar.gz

2. After downloading

tar -zxvf go1.9.2.linux-amd64.tar.gz Unzip the source package

3. Move to /usr/local/go which is GOROOT

4. Set GOPATH and PATH environment variables

export GOROOT=/usr/local/go #设置为go安装的路径
export GOPATH=$HOME/gocode #默认安装包的路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

View Linux go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/gocode"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build057487015=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

go env

For more golang knowledge, please pay attention to the

golang tutorial column.

The above is the detailed content of Detailed explanation of GOROOT, GOPATH and GOBIN in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete