分享
介绍一些有助于写出更好Go程序的工具
xcltapestry · · 4493 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
前面讲了测试和性能调优之类,这篇主要讲如何利用Go提供的一些工具和参数,帮助写出更好的代码.
一. golint 检查代码规范
二. go vet 检查代码存在的隐患
三. -race 检查是否有race condition
二. go vet 检查代码存在的隐患
三. -race 检查是否有race condition
这几个比较常用,当然还有其它很多的辅助工具就不在这一一列举了.
一. golint 检查代码规范
二. go vet 检查代码存在的隐患
三. -race 检查是否有race condition
一. golint 检查代码规范
/*
golint 例子
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"fmt"
)
const fooId = "blah"
var var_name int
type hidden struct{}
func Exported() hidden {
return hidden{}
}
type T struct{}
func main() {
Exported()
}
/*
E:\GOtest\testing\testlint>golint -h
Usage of golint:
golint [flags] # runs on package in current directory
golint [flags] package
golint [flags] directory
golint [flags] files... # must be a single package
Flags:
-min_confidence float
minimum confidence of a problem to print it (default 0.8)
E:\GOtest\testing\testlint>dir
驱动器 E 中的卷是 doc
卷的序列号是 0E3D-2A1F
E:\GOtest\testing\testlint 的目录
2015年11月22日 21:00 <DIR> .
2015年11月22日 21:00 <DIR> ..
2015年11月22日 20:57 6,448,128 golint.exe
2015年11月22日 21:41 1,066 main.go
2 个文件 6,449,194 字节
2 个目录 15,122,874,368 可用字节
E:\GOtest\testing\testlint>golint main.go
main.go:15:7: const fooId should be fooID
main.go:17:5: don't use underscores in Go names; var var_name should be varName
main.go:21:1: exported function Exported should have comment or be unexported
main.go:21:17: exported func Exported returns unexported type main.hidden, which can be annoying to use
main.go:25:6: exported type T should have comment or be unexported
E:\GOtest\testing\testlint>
*/列出了具体到哪一行,可能存在的问题.二. go vet 检查代码存在的隐患
/*
go vet 例子
go doc cmd/vet
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"fmt"
)
func main() {
var c string
fmt.Sprintf("xxx s", c)
}
/*
E:\GOtest\testing\testvet>dir
驱动器 E 中的卷是 doc
卷的序列号是 0E3D-2A1F
E:\GOtest\testing\testvet 的目录
2015年11月22日 21:47 <DIR> .
2015年11月22日 21:47 <DIR> ..
2015年11月22日 21:47 98 main.go
1 个文件 98 字节
2 个目录 15,122,874,368 可用字节
E:\GOtest\testing\testvet>go vet
main.go:9: result of fmt.Sprintf call not used
main.go:9: no formatting directive in Sprintf call
exit status 1
*/
三. -race 检查是否有race condition
/*
race例子
go run -race trace.go
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"time"
)
var (
g int
)
func main() {
go func() {
g++
}()
go func() {
g = g + 2
}()
time.Sleep(1 * time.Second)
}
/*
E:\GOtest\testing\testvet>go run trace.go
E:\GOtest\testing\testvet>go run -race trace.go
==================
WARNING: DATA RACE
Write by goroutine 6:
main.main.func2()
E:/GOtest/testing/testvet/trace.go:27 +0x37
Previous write by goroutine 5:
main.main.func1()
E:/GOtest/testing/testvet/trace.go:23 +0x53
Goroutine 6 (running) created at:
main.main()
E:/GOtest/testing/testvet/trace.go:28 +0x57
Goroutine 5 (finished) created at:
main.main()
E:/GOtest/testing/testvet/trace.go:24 +0x3f
==================
Found 1 data race(s)
exit status 66
E:\GOtest\testing\testvet>
*/
这几个比较常用,当然还有其它很多的辅助工具就不在这一一列举了.
BLOG: http://blog.csdn.net/xcl168
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信4493 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
一. golint 检查代码规范
二. go vet 检查代码存在的隐患
三. -race 检查是否有race condition
一. golint 检查代码规范
/*
golint 例子
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"fmt"
)
const fooId = "blah"
var var_name int
type hidden struct{}
func Exported() hidden {
return hidden{}
}
type T struct{}
func main() {
Exported()
}
/*
E:\GOtest\testing\testlint>golint -h
Usage of golint:
golint [flags] # runs on package in current directory
golint [flags] package
golint [flags] directory
golint [flags] files... # must be a single package
Flags:
-min_confidence float
minimum confidence of a problem to print it (default 0.8)
E:\GOtest\testing\testlint>dir
驱动器 E 中的卷是 doc
卷的序列号是 0E3D-2A1F
E:\GOtest\testing\testlint 的目录
2015年11月22日 21:00 <DIR> .
2015年11月22日 21:00 <DIR> ..
2015年11月22日 20:57 6,448,128 golint.exe
2015年11月22日 21:41 1,066 main.go
2 个文件 6,449,194 字节
2 个目录 15,122,874,368 可用字节
E:\GOtest\testing\testlint>golint main.go
main.go:15:7: const fooId should be fooID
main.go:17:5: don't use underscores in Go names; var var_name should be varName
main.go:21:1: exported function Exported should have comment or be unexported
main.go:21:17: exported func Exported returns unexported type main.hidden, which can be annoying to use
main.go:25:6: exported type T should have comment or be unexported
E:\GOtest\testing\testlint>
*/列出了具体到哪一行,可能存在的问题.二. go vet 检查代码存在的隐患
/*
go vet 例子
go doc cmd/vet
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"fmt"
)
func main() {
var c string
fmt.Sprintf("xxx s", c)
}
/*
E:\GOtest\testing\testvet>dir
驱动器 E 中的卷是 doc
卷的序列号是 0E3D-2A1F
E:\GOtest\testing\testvet 的目录
2015年11月22日 21:47 <DIR> .
2015年11月22日 21:47 <DIR> ..
2015年11月22日 21:47 98 main.go
1 个文件 98 字节
2 个目录 15,122,874,368 可用字节
E:\GOtest\testing\testvet>go vet
main.go:9: result of fmt.Sprintf call not used
main.go:9: no formatting directive in Sprintf call
exit status 1
*/
三. -race 检查是否有race condition
/*
race例子
go run -race trace.go
Author: xcl
Date: 2015年11月22日
*/
package main
import (
"time"
)
var (
g int
)
func main() {
go func() {
g++
}()
go func() {
g = g + 2
}()
time.Sleep(1 * time.Second)
}
/*
E:\GOtest\testing\testvet>go run trace.go
E:\GOtest\testing\testvet>go run -race trace.go
==================
WARNING: DATA RACE
Write by goroutine 6:
main.main.func2()
E:/GOtest/testing/testvet/trace.go:27 +0x37
Previous write by goroutine 5:
main.main.func1()
E:/GOtest/testing/testvet/trace.go:23 +0x53
Goroutine 6 (running) created at:
main.main()
E:/GOtest/testing/testvet/trace.go:28 +0x57
Goroutine 5 (finished) created at:
main.main()
E:/GOtest/testing/testvet/trace.go:24 +0x3f
==================
Found 1 data race(s)
exit status 66
E:\GOtest\testing\testvet>
*/
这几个比较常用,当然还有其它很多的辅助工具就不在这一一列举了.
BLOG: http://blog.csdn.net/xcl168