golint 执行不返回任何内容

我正在为exercism.io 编写练习。我的导师说我的代码应该由一些 go lint 来检查,他建议我尝试 golint 和 golangci-lint。我通过安装 golintgo get -u golang.org/x/lint/golint并启动它:


rustam:hamming $ golint hamming.go 

rustam:hamming $ 

但它什么也不返回。此外,如果我使用 golangci-lint 也会得到相同的结果——什么都没有。我不知道为什么。


有我的代码,充满了风格错误:


// Package hamming is Exercism.io exercise

package hamming


import "errors"


// Distance — Calculating Hamming Distance for two DNA strands

func Distance(a, b string) (int, error) {


    if len(a) != len(b) {

        return 0, errors.New("Strands should be equal size")

    }


    if len(a) == 0 || len(b) == 0 {

        return 0, nil

    }


    var hd int = 0


    for i := 0; i < len(a); i++ {

        if a[i] != b[i] {

            hd++

        }

    }


    return hd, nil

}

还有我的 go env:


rustam:hamming $ go env

GO111MODULE=""

GOARCH="amd64"

GOBIN="/Users/rustam/go/bin"

GOCACHE="/Users/rustam/Library/Caches/go-build"

GOENV="/Users/rustam/Library/Application Support/go/env"

GOEXE=""

GOFLAGS=""

GOHOSTARCH="amd64"

GOHOSTOS="darwin"

GONOPROXY=""

GONOSUMDB=""

GOOS="darwin"

GOPATH="/Users/rustam/go"

GOPRIVATE=""

GOPROXY="https://proxy.golang.org,direct"

GOROOT="/usr/local/Cellar/go/1.13.4/libexec"

GOSUMDB="sum.golang.org"

GOTMPDIR=""

GOTOOLDIR="/usr/local/Cellar/go/1.13.4/libexec/pkg/tool/darwin_amd64"

GCCGO="gccgo"

AR="ar"

CC="clang"

CXX="clang++"

CGO_ENABLED="1"

GOMOD=""

CGO_CFLAGS="-g -O2"

CGO_CPPFLAGS=""

CGO_CXXFLAGS="-g -O2"

CGO_FFLAGS="-g -O2"

CGO_LDFLAGS="-g -O2"

PKG_CONFIG="pkg-config"

GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4j/4rjv22zs1pb68wssv5gn2wfw0000gn/T/go-build148685464=/tmp/go-build -gno-record-gcc-switches -fno-common"

和我的 ~/.bash_profile


export GOPATH=$HOME/go

export GOBIN=$HOME/go/bin

export PATH=$PATH:$GOPATH/bin

如果您能指出我的错误,我将不胜感激。


海绵宝宝撒
浏览 241回答 2
2回答

慕容森

在 Go 中,最少的检查可能是 Go 工具go fmt、、、go vet和golint。这是一个最小的、可重现的示例(请参阅如何创建一个最小的、可重现的示例),它表明您的hamming.go程序通过了所有当前检查。命令 golint$ go versiongo version devel +075c20cea8 Thu Dec 26 13:53:31 2019 +0000 linux/amd64$ go fmt hamming.go$ go vet hamming.go$ go get -u golang.org/x/lint/golintgo: downloading golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05fgo: found golang.org/x/lint/golint in golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05fgo: downloading golang.org/x/tools v0.0.0-20191125144606-a911d9008d1fgo: golang.org/x/tools upgrade => v0.0.0-20191226230302-065ed046f11ago: downloading golang.org/x/tools v0.0.0-20191226230302-065ed046f11a$ golint hamming.go$ cat hamming.go// Package hamming is Exercism.io exercisepackage hammingimport "errors"// Distance — Calculating Hamming Distance for two DNA strandsfunc Distance(a, b string) (int, error) {&nbsp; &nbsp; if len(a) != len(b) {&nbsp; &nbsp; &nbsp; &nbsp; return 0, errors.New("Strands should be equal size")&nbsp; &nbsp; }&nbsp; &nbsp; if len(a) == 0 || len(b) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return 0, nil&nbsp; &nbsp; }&nbsp; &nbsp; var hd int = 0&nbsp; &nbsp; for i := 0; i < len(a); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if a[i] != b[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hd++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return hd, nil}$评论:我的导师发送了他的 linter 执行,并且有一些消息,例如$ golint hamming.go hamming.go:17:15:&nbsp;should drop = 0 from declaration of var hd; it is the zero value&nbsp;– 锈迹斑斑您的导师正在使用旧版本的golint. 对于当前版本,它们应该运行go get -u golang.org/x/lint/golint您的导师是否解释了 的有限目的golint?例如,“Golint 并不完美,同时存在误报和误报。不要将其输出视为黄金标准。”Golint 是 Go 源代码的 linter。目的Golint 与 gofmt 不同。Gofmt 重新格式化 Go 源代码,而 golint 打印出样式错误。Golint 与 govet 不同。Govet 关注正确性,而 golint 关注编码风格。Golint 在 Google 中使用,它寻求与开源 Go 项目的公认风格相匹配。golint 提出的建议正是:建议。Golint 并不完美,同时存在误报和误报。不要将其输出视为黄金标准。我们不会添加编译指示或其他旋钮来抑制特定警告,因此不要期望或要求代码完全“无 lint”。简而言之,这个工具不是而且永远不会足够值得信赖,以至于它的建议可以自动执行,例如作为构建过程的一部分。Golint 为 Effective Go 和 CodeReviewComments wiki 页面中列出的许多机械检查项目提供建议。这是我的解决方案尝试:package hammingimport "errors"// Distance returns the Hamming distance for two DNA strings of equal length.// DNA strings are constructed from the alphabet {A, C, G, T}.func Distance(a, b string) (int, error) {&nbsp; &nbsp; if len(a) != len(b) {&nbsp; &nbsp; &nbsp; &nbsp; return 0, errors.New("strings should be of equal length")&nbsp; &nbsp; }&nbsp; &nbsp; d := 0&nbsp; &nbsp; for i := 0; i < len(a); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if a[i] != b[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return d, nil}

潇潇雨雨

您应该定义置信水平(min_confidence参数)。例如:golint -min_confidence 0 your_file_to_test查看 golint 用法:golint -hUsage of golint:&nbsp; &nbsp; golint [flags] # runs on package in current directory&nbsp; &nbsp; golint [flags] [packages]&nbsp; &nbsp; golint [flags] [directories] # where a '/...' suffix includes all sub-directories&nbsp; &nbsp; golint [flags] [files] # all must belong to a single packageFlags:&nbsp; -min_confidence float&nbsp; &nbsp; &nbsp; &nbsp; minimum confidence of a problem to print it (default 0.8)&nbsp; -set_exit_status&nbsp; &nbsp; &nbsp; &nbsp; set exit status to 1 if any issues are found
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go