golang install, plug-ins and built-in commands

GO Configuration

installation

  • apt installation
    • apt update && apt install -y golang
    • Uninstall using the apt remove command, then use apt autoremove remove dependencies
    • Note that this method can only install version 1.6. For later (such as the need context Pack), install in accordance with the second approach
  • Installation package
  • If the downloaded version 1.13 and above can change the default proxy
    • You do not have to download plug-back manually installed
    • GOPRIVATE can set up a private library in the organization
# vim ~/.profile
export GOPROXY="https://goproxy.cn,direct"
export GOPRIVATE="*.xxx.org"
export GOSUMDB="golang.google.cn"

# source ~/.profile

Multi-Versioning

  • Quick to install and manage multiple versions of the following script golang
    • You need to /usr/localcreate a directory go-dirand go-tartwo directories
    • If you need it in Linux darwin into linux
BigVersion=$1
SmallVersion=$2

RootPath=/usr/local
DirPath=/usr/local/go-dir/go-${BigVersion}-${SmallVersion}
TarFile=go1.${BigVersion}.${SmallVersion}.darwin-amd64.tar.gz

wget  https://golang.google.cn/dl/${TarFile}
mkdir -p ${DirPath}
tar -C ${DirPath} -xzf ${TarFile}
cd ${RootPath}; rm go
ln -s ${DirPath}/go/ ${RootPath}/go

Configuration environment variable

Here Insert Picture Description

  • / etc / profile file the entry into force of the entire computer
  • ~ / .profile or ~. / bash_profile file for the user to take effect
  • If you use zsh, then it was loaded in the / etc / zsh / zshrc file
# apt安装的不用配置该项
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

# 这个是你自己的开发目录
# 可以同时存在多个开发目录,用:连接
export GOPATH=$HOME/vscode/go-config
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

# 如果原先有老得go环境变量,还要删除
export PATH=${PATH#/*/tiger/go/go/bin}
  • By configuring the effective
    • source /etc/profile orsource ~/.profile

Plug (vscode)

GO built-in command

Command List

	build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	env         print Go environment information
	bug         start a bug report
	fix         run go tool fix on packages
	fmt         run gofmt on package sources
	generate    generate Go files by processing source
	get         download and install packages and dependencies
	install     compile and install packages and dependencies
	list        list packages
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         run go tool vet on packages

go build

  • If the target has a main bag package, it will generate a binary file
    • If there is no main package, build instructions can only be used by the compiler to check whether, after compilation package will drop all temporary objects generated at compile time
  • The default scope is the current folder, you can also specify a directory compiled
    • ...All matches
      • go build flysnow.org/tools/...All packages in the compilation tools directory
  • Generates a binary file in the current directory
    • Use the -o parameter to explicitly specify the path and file name
  • - raceOpen competition assay parameters
  • Cross-platform compilation
    • GOOS=linux GOARCH=amd64 go build packageName

go clean

  • Delete compiled files

go install

  • Steps to compile and install the file, which installed more than build a
  • Do not specify the default package for the installation of the current package
  • Installed or under bin pkg directory
    • Package file containing main (executable file) is mounted to the $GOPATH/bindirectory
    • Normal package file (file generation .a) mounted to the $GOPATH/pkg/$GOOS/directory
  • When there are multiple GOPATH directory, only the first valid directory

go run

  • Compile and execute programs that do not generate the file
  • File must contain the main function as a parameter

go get

  • Remote Access Code package and install
    • The source code is downloaded to $GOPATH/srcthe directory
  • -v display process
  • -u update with the network
  • -d Download only, do not install
  • go get automatically detect and install all dependencies
    • To get all the dependencies of a project, do not use git clone, go get used
# 下载所有依赖
go get -v ./...

go help

  • View built-in commands using the help
    • E.ggo help build

go env

  • No arguments is the viewing environment parameters
  • -w set global environment variables
    • E.g go env -w GOBIN=$HOME/bin

go vet

  • Static code checking

reference

Geek Academy - Command-line tools

Know almost - the command-line tools

Published 161 original articles · won praise 19 · views 50000 +

Guess you like

Origin blog.csdn.net/winter_wu_1998/article/details/101705042