Go - go vet的用法

Posted 王万林 Ben

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go - go vet的用法相关的知识,希望对你有一定的参考价值。

Go - go vet的用法

go vet介绍

Vet examines Go source code and reports suspicious constructs, such as
Printf calls whose arguments do not align with the format string. Vet uses
heuristics that do not guarantee all reports are genuine problems, but it
can find errors not caught by the compilers.

Vet is normally invoked through the go command. This command vets the
package in the current directory:

    go vet

whereas this one vets the packages whose path is provided:

    go vet my/project/...
    

文档中可以了解到,go vet命令可以用来检测错误。

╭─ben at MacBook Pro in ~/go/src/github.com/goinaction/code/chapter3/tmp on master✘✘✘ 2021/08/29 - 15:28:58
╰─⠠⠵ cat go_vet.go
package main

import "fmt"

func main() {
        fmt.Printf("The quick brown fox jumped over lazy dogs", 3.14)
}
╭─ben at MacBook Pro in ~/go/src/github.com/goinaction/code/chapter3/tmp on master✘✘✘ 2021/08/29 - 15:29:05
╰─⠠⠵ go vet go_vet.go 
# command-line-arguments
./go_vet.go:7:19: Printf call has arguments but no formatting directives

后记

保持一个良好的习惯:在提交到仓库之前,最好要运行对你的code base执行go vet扫错。

以上是关于Go - go vet的用法的主要内容,如果未能解决你的问题,请参考以下文章

解决go: go.mod file not found in current directory or any parent directory; see ‘go help modules‘(代码片段

你知道的Go切片扩容机制可能是错的

[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础

go lint 工具范围关闭检测

Go 1.19 发行说明(翻译)

Go 1.19 发行说明(翻译)