Go语言自学系列 | go常用命令
Posted COCOgsta
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go语言自学系列 | go常用命令相关的知识,希望对你有一定的参考价值。
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,请移步至知乎网站,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/484035621
查看可用命令
直接在终端中输入 go help 即可显示所有的 go 命令以及相应命令功能简介,主要有下面这些:
- build: 编译包和依赖
- clean: 移除对象文件
- doc: 显示包或者符号的文档
- env: 打印go的环境信息
- bug: 启动错误报告
- fix: 运行go tool fix
- fmt: 运行gofmt进行格式化
- generate: 从processing source生成go文件
- get: 下载并安装包和依赖
- install: 编译并安装包和依赖
- list: 列出包
- run: 编译并运行go程序
- test: 运行测试
- tool: 运行go提供的工具
- version: 显示go的版本
- vet: 运行go tool vet
参考文档
https://golang.google.cn/doc/cmd
常用命令举例
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go build .\\test.go
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go run .\\test.go
hello
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\\Users\\guoliang\\AppData\\Local\\go-build
set GOENV=C:\\Users\\guoliang\\AppData\\Roaming\\go\\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\\Users\\guoliang\\go\\pkg\\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\\Users\\guoliang\\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
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.16.3
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\\guoliang\\AppData\\Local\\Temp\\go-build3337909645=/tmp/go-build -gno-record-gcc-switches
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go help
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help <command>" for more information about a command.
Additional help topics:
buildconstraint build constraints
buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
packages package lists and patterns
private configuration for downloading non-public code
testflag testing flags
testfunc testing functions
vcs controlling version control with GOVCS
Use "go help <topic>" for more information about that topic.
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go get -u github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.7.7
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/json-iterator/go v1.1.9
go: downloading github.com/go-playground/validator/v10 v10.4.1
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/ugorji/go v1.1.7
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading github.com/mattn/go-isatty v0.0.14
go: downloading github.com/json-iterator/go v1.1.12
go: downloading golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: downloading github.com/go-playground/validator/v10 v10.10.0
go: downloading github.com/ugorji/go/codec v1.2.6
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading github.com/go-playground/validator v9.31.0+incompatible
go: downloading github.com/ugorji/go v1.2.6
go: downloading github.com/golang/protobuf v1.5.2
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading github.com/go-playground/universal-translator v0.17.0
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/go-playground/universal-translator v0.18.0
go: downloading github.com/go-playground/locales v0.13.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/leodido/go-urn v1.2.1
go: downloading golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2
go: downloading github.com/go-playground/locales v0.14.0
go: downloading google.golang.org/protobuf v1.26.0
go: downloading google.golang.org/protobuf v1.27.1
go: downloading golang.org/x/text v0.3.6
# 创建test.go文件添加一个包
// 主包,可执行文件所在包
package main
// 导入包
import (
"fmt"
"github.com/gin-gonic/gin"
)
// 主函数,入口函数
func main()
// 打印输入
fmt.Println("hello")
r := gin.Default()
r.GET("/ping", func(c *gin.Context)
c.JSON(200, gin.H
"message": "pong",
)
)
r.Run()
# 执行go mod tidy,自动引入包
// 主包,可执行文件所在包
package main
// 导入包
PS D:\\SynologyDrive\\软件开发\\go\\golang入门到项目实战> go mod tidy
go: finding module for package github.com/gin-gonic/gin
go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.7.7
go: downloading github.com/stretchr/testify v1.4.0
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading github.com/go-playground/assert/v2 v2.0.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
# go.mod文件自动更新
require github.com/gin-gonic/gin v1.7.7
以上是关于Go语言自学系列 | go常用命令的主要内容,如果未能解决你的问题,请参考以下文章