Go的下载与安装

首先下载Go,下载链接https://golang.google.cn/dl/

go version
go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Dell\AppData\Local\go-build
set GOENV=C:\Users\Dell\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Dell\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Dell\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.19.1
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\Dell\AppData\Local\Temp\go-build1298709145=/tmp/go-build -gno-record-gcc-switches

输出信息部分如下:

GOARCHGOBINGOOSGOPATHC:\Users\Dell\goGOROOT
GOPATHGOPATH
平台GOPATH默认值举例
Windows平台%USERPROFILE%/goC:\Users\用户名\go
Unix平台$HOME/go/home/用户名/go
GOPATH
$HOME/go
  --bin  # 存放编译后的可执行文件
  --pkg  # 依赖包编译后的*.a文件
  --src  # 存放源码文件,以代码包为组织形式

比如下面就是一般Go项目的开发目录:

my-go                 // my-go为GOPATH目录
  -- bin
     -- myApp1        // 编译生成
     -- myApp2        // 编译生成
     -- myApp3        // 编译生成
  -- pkg                             依赖包编译后的*.a文件//
  -- src
     -- MyApp1        // 项目1
        -- models
        -- controllers
        -- others
        -- main.go 
     -- MyApp2        // 项目2
        -- models
        -- controllers
        -- others
        -- main.go
GOPATHGOPATHC:\Users\Dell\goC:\Users\Dell\Desktop\my-go\src\MyApp1import
import ”MyApp1\models“ // models为包名
import
import
$GOROOT\src/C:\Program Files\Go$GOROOTgo envGOROOT$GOROOTProject GOPATH\src\C:\Users\Dell\Desktop\my-go\src\MyApp1Project GOPATH\src\Global GOPATHC:\Users\Dell\go$GOPATHgo envGOPATH
HelloWorld
go mod init Go_1go.mod
module GO_1

go 1.19
go.mod
go mod init
HelloWorld.go
package main //指明这个文件属于main包

import "fmt" //导入fmt包

func main() {
    fmt.Println("Hello, World!") //输出Hello, World!
}
go run .go build HelloWorld.goHelloWorld.exe
参考文献