当前位置:网站首页>go语言笔记(4)go常用管理命令
go语言笔记(4)go常用管理命令
2022-07-04 19:03:00 【fiveym】
介绍
在Go语言中,我们很多操作都是通过 go 命令进行的,比如我们要执行go文件的编译,就需要使用 go build 命令,除了 build 命令之外,还有很多常用的命令,这一次我们就统一进行介绍,对常用命令有一个了解,这样我们就可以更容易的开发我们的Go程序了。
二级标题
go version: 获取Go版本
go env: 查看Go环境变量
go help: 查看Go帮助命令
go get: 获取远程包(需提前安装git或hg)
go build: 编译并生成可执行程序
go run: 直接运行程序
go fmt: 格式化源码
go install: 编译包文件以及整个程序
go test: go原生提供的单元测试命令
go clean: 移除当前源码包和关联源码包里编译生成的文件
go tool: 升级Go版本时,修复旧版代码
godoc -http:80:开启一个本地80端口的web文档
gdb 可执行程序名:调试Go编译出来的文件
使用格式:
go command [arguments]``` ## go bulid go build ,它可以启动编译,把我们的包和相关的依赖编译成 一个可执行的文件 。 如果是main包,执行go build之后,会在当前目录下生成一个可执行文件。 示例: ```go
// 编译当前目录为一个可执行文件
go build
// 编译当前目录为一个可执行文件
go build .
// 编译hello.go文件为一个可执行文件
go build hello.g
如果go bulid报错
/usr/lib/go-1.10/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/tmp/go-link-008006128/000026.o: In function _cgo_05dc84caff82_Cfunc_sysconf': /tmp/go-build/cgo-gcc-prolog:32: multiple definition of_cgo_05dc84caff82_Cfunc_sysconf’ /tmp/go-link-008006128/000024.o:/tmp/go-build/cgo-gcc-prolog:32: first defined here collect2: error: ld returned 1 exit status
解决方法:
linux: export CGO_ENABLED=0
windows : cmd: set CGO_ENABLED=0
windows : powershell:$env:CGO_ENABLED=0
go run
go bulid是先编译,然后我们在执行可以执行文件来执行我们的程序需要两步
go run 这个命令就是可以把这两步合成一步的命令,节省我们录入的时间
命令格式:
```go
go run 文件名.go
示例:
go run main.go
go install
go install命令和go bulid 命令类似用来生成项目可执行文件,不过它在编译后,吧生成的可执行文件或者库安装到对应的目录下,以供使用。
可执行文件安装到: $GOPATH/bin/
可引用库安装到: $GOPATH/pkg/
注意: 没有开启go mod时,使用上述命令需要配置GOPATH,否则会报:no install location。
go get
go get用来获取远程仓库中的包,使用该命令前必须配置GOPATH,而且依据不同的源码网站,还要安装不同的版本管理工具,比如从github上使用go get需要额外安装git。
go get本质上可以理解为通过源码工具clone下代码后,执行了go install。
命令格式:
下载包:添加 -u 参数可以自动更新包和依赖
go get github.com/**/**
使用安装的包
import "github.com/****/****"
如果下载慢,记得配置goproxy
// 设置module管理模式
$ go env -w GO111MODULE=on
//设置下载源
$ go env -w GOPR
go fmt
go fmt命令可以格式化代码文件
使用go fmt命令,其实是调用了gofmt,而且需要参数-w,否则格式化结果不会写入文件
//命令格式:
go fmt -w 文件名.go
//示例:格式化整个项目
gofmt -w -l src
//常见参数:
-l: 显示那些需要格式化的文件
-w: 把改写后的内容直接写入到文件中,而不是作为结果打印到标准输出。
-r: 添加形如"a[b:len(a)] -> a[b:]"的重写规则,方便我们做批量替换
-s: 简化文件中的代码
-d: 显示格式化前后的diff而不是写入文件,默认是false
-e: 打印语法错误到标准输出。无此参数只会打印不同行的前10个错误。
go tool
go tool下聚集了很多命令,主要有2个,即fix和vet:
go tool fix .:用来修复以前老版本的代码到新版本
go tool vet directory|files:分析当前目录的代码是否正确
边栏推荐
- 原来这才是 BGP 协议
- Optimize if code with policy mode [policy mode]
- So this is the BGP agreement
- Employment prospects and current situation of Internet of things application technology
- 易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
- Swagger suddenly went crazy
- How is the entered query SQL statement executed?
- Chrome development tool: what the hell is vmxxx file
- HMM hidden Markov model and code implementation
- Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
猜你喜欢
What is involution?
ACM组合计数入门
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Actual combat simulation │ JWT login authentication
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
YOLOv5s-ShuffleNetV2
Selected review | machine learning technology for Cataract Classification / classification
node强缓存和协商缓存实战示例
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
随机推荐
最长的可整合子数组的长度
同事的接口文档我每次看着就头大,毛病多多。。。
太方便了,钉钉上就可完成代码发布审批啦!
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
Actual combat simulation │ JWT login authentication
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
Form组件常用校验规则-1(持续更新中~)
In operation (i.e. included in) usage of SSRs filter
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
Six stones programming: about code, there are six triumphs
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
Prometheus installation
Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
Dark horse programmer - software testing - stage 07 2-linux and database -09-24-linux command learning steps, wildcards, absolute paths, relative paths, common commands for files and directories, file
HMM hidden Markov model and code implementation
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Dynamic memory management