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

 前面讲了测试和性能调优之类,这篇主要讲如何利用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


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值