Build fast, reliable, and efficient software at scale
- Go is an open source programming language supported by Google
- Easy to learn and get started with
- Built-in concurrency and a robust standard library
- Growing ecosystem of partners, communities, and tools
1.官网下载
https://golang.google.cn/dl/
安装完成后,命令行输入go version可以查看是否安装成功
xxxx> go version
go version go1.17.7 windows/amd64
go env可以查看当前生效的go环境参数配置
xxxx> go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=xxx\AppData\Local\go-build
set GOENV=xxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=xxx\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=xxx\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
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
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ 末小七\AppData\Local\Temp\go-build2034886303=/tmp/go-build -gno-record-gcc-switches
Go 语言安装根目录的路径,也就是 GO 语言的安装路径。
若干工作区目录的路径。是我们自己定义的工作空间。
1.8版本以前必须配置这个环境变量,1.8之后如果没有使用默认值
windows默认值是%USERPROFILE%/go
Unix:$HOME/go
Mac:可以通过修改~/.bash_profile设置
GO 程序生成的可执行文件(executable file)的路径。
GO111MODULE设置为on
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OmqoZpPf-1646819609778)(image-20220309144929258.png)]
package main
import "fmt"
func main() {
fmt.Println("hello world~")
}
在java中,package名字跟目录名字是要保持一致的,但是go是没有这个约束的。
func main() {
fmt.Println("hello world~")
os.Exit(-1)
}
##########################################
hello world~
Process finished with the exit code -1
func main() {
if len(os.Args)>1 {
fmt.Println("args:",os.Args[1])
}
fmt.Println("hello world~")
os.Exit(0)
}
##########################################
D:\gocode\godemo\basic\firstfunc> go run hello.go test
args: test
hello world~
运行
编译go文件为二进制文件,go默认使用静态链接,编译完后只是个独立的二进制文件,可以copy到不同的机器上运行。在安装部署的时候,非常便携。
package test
import "testing"
func TestHello(t *testing.T){
t.Log("hello world test~")
}
##########################################
=== RUN TestHello
hello_test.go:6: hello world test~
--- PASS: TestHello (0.00s)
PASS
const (
Monday = iota + 1
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
func TestIota(t *testing.T) {
fmt.Println("Monday:", Monday)
fmt.Println("Tuesday:", Tuesday)
fmt.Println("Wednesday:", Wednesday)
fmt.Println("Thursday:", Thursday)
fmt.Println("Friday:", Friday)
fmt.Println("Saturday:", Saturday)
fmt.Println("Sunday:", Sunday)
}
##########################################
=== RUN TestIota
Monday: 1
Tuesday: 2
Wednesday: 3
Thursday: 4
Friday: 5
Saturday: 6
Sunday: 7
--- PASS: TestIota (0.00s)
PASS