分享
  1. 首页
  2. 文章

介绍一些有助于写出更好Go程序的工具

xcltapestry · · 4493 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

前面讲了测试和性能调优之类,这篇主要讲如何利用Go提供的一些工具和参数,帮助写出更好的代码.
一. 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



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:xcltapestry

查看原文:介绍一些有助于写出更好Go程序的工具

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
4493 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏