Introduce some tools to help you write better go programs

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. In front of the test and performance tuning, this article focuses on using some of the tools and parameters that go provides to help write better code.
I. Golint INSPECTION CODE specification
Two. Go vet check code for pitfalls
Three.-race check if there is race condition


I. Golint INSPECTION CODE specification

/*golint Example Author:xcldate:2015-11-22*/package mainimport ("FMT") const FOOID = "blah" var var_name inttype Hidden struct{} Func exported () hidden {return hidden{}}type T Struct{}func main () {exported ()}/*e:\gotest\testing\testlint>golint-  Husage of Golint:golint [flags] # runs on the package in the current directory Golint [flags] Package Golint [Flags] directory Golint [flags] files ... # must is a single packageflags:-min_confidence float minimum C Onfidence of a problem to print it (default 0.8) E:\gotest\testing\testlint>dir the volume in drive E is the DOC volume serial number is 0e3d-2a1f E:\GOtes T\testing\testlint directory 2015/11/22 21:00 <DIR>. 2015/11/22 21:00 <DIR> ... 2015/11/22 20:57 6,448,128 golint.exe2015/11/22 21:41 1,066 main.go 2 files 6,449,1 94 Bytes 2 Directories 15,122,874,368 Available Bytes E:\gotest\testing\testlint>golint main.gomain.go:15:7: const fooid should Be fooidmain.go:17:5: Don ' t use UnderscoRes in Go names; var var_name should be varnamemain.go:21:1: exported function exported should has comment or be unexportedmain.go:21:17: Exported func exported returns unexported type Main.hidden, which can be annoying to Usemain.go:25:6: exported type T Shou LD has comment or be unexportede:\gotest\testing\testlint>*/
Lists the specific lines to which the problem may exist.

two. Go vet check code for pitfalls

/*go Vet Example Go doc cmd/vetauthor:xcldate:2015-11-22*/package mainimport ("FMT") func main () {var c stringfmt. Sprintf ("xxx S", c)}/*e:\gotest\testing\testvet>dir the volume in drive E is the doc volume's serial number is the directory of 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 Files             98 bytes               2 directories 15,122,874,368 Available Bytes e:\gotest\testing\testvet> Go vetmain.go:9: Result of FMT. Sprintf Call does Usedmain.go:9: no formatting directive in Sprintf callexit status 1*/

three.-race Check if there is race condition

/*race example Go run-race trace.goauthor:xcldate:2015-11-22*/package mainimport ("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.goe:\gotest\testing\testvet>go run-race trace.go=========== =======warning:data Racewrite by Goroutine 6:  main.main.func2 ()      e:/gotest/testing/testvet/trace.go:27 + 0x37previous Write by Goroutine 5:  main.main.func1 ()      e:/gotest/testing/testvet/trace.go:23 +0x53goroutine 6 ( Running) created at:  main.main ()      e:/gotest/testing/testvet/trace.go:28 +0x57goroutine 5 (finished) created at :  main.main ()      e:/gotest/testing/testvet/trace.go:24 +0x3f==================found 1 Data race (s) exit status 66e:\gotest\testing\testvet>*/

These are more commonly used, of course, there are many other auxiliary tools are not listed here.


blog:http://blog.csdn.net/xcl168


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.