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:
D:\project\src\go_dev\day1\package_example\main>go run main.go 400 100Now 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\mainSo 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 installgo install
go install will generate an executable file and place it directly in the bin directory. Of course, this There are prerequisitesWhat 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 .aAbout 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.gz2. After downloading tar -zxvf go1.9.2.linux-amd64.tar.gz Unzip the source package3. Move to /usr/local/go which is GOROOT4. Set GOPATH and PATH environment variablesexport 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 envFor 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!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

闭包(closure)是一个函数以及其捆绑的周边环境状态(lexical environment,词法环境)的引用的组合。 换而言之,闭包让开发者可以从内部函数访问外部函数的作用域。 闭包会随着函数的创建而被同时创建。

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.