golint 执行 returns 无

golint execution returns nothing

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

rustam:hamming $ golint hamming.go 
rustam:hamming $ 

但它 returns 什么都没有。 此外,如果我使用 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
}

这是我的环境:

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

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

在 Go 中,最少的检查可能是 Go 工具 go fmtgo vetgolint

这是一个最小的、可重现的示例(参见 How to create a Minimal, Reproducible Example),表明您的 hamming.go 程序通过了所有当前检查。

Command golint

$ go version
go 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/golint
go: downloading golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f
go: found golang.org/x/lint/golint in golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f
go: downloading golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f
go: golang.org/x/tools upgrade => v0.0.0-20191226230302-065ed046f11a
go: downloading golang.org/x/tools v0.0.0-20191226230302-065ed046f11a
$ golint hamming.go
$ cat hamming.go
// 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
}
$

Comment: My mentor send his linter execution and there was some messages like

$ golint hamming.go hamming.go:17:15: 
should drop = 0 from declaration of var hd; it is the zero value 

– Rustery

您的导师使用的是 golint 的过时版本。对于当前版本,他们应该 运行

go get -u golang.org/x/lint/golint

您的导师是否解释过 golint 的有限用途?例如,"Golint is not perfect, and has both false positives and false negatives. Do not treat its output as a gold standard."

Golint is a linter for Go source code.

Purpose

Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes.

Golint differs from govet. Govet is concerned with correctness, whereas golint is concerned with coding style. Golint is in use at Google, and it seeks to match the accepted style of the open source Go project.

The suggestions made by golint are exactly that: suggestions. Golint is not perfect, and has both false positives and false negatives. Do not treat its output as a gold standard. We will not be adding pragmas or other knobs to suppress specific warnings, so do not expect or require code to be completely "lint-free". In short, this tool is not, and will never be, trustworthy enough for its suggestions to be enforced automatically, for example as part of a build process. Golint makes suggestions for many of the mechanically checkable items listed in Effective Go and the CodeReviewComments wiki page.


这是我尝试的解决方案:

package hamming

import "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) {
    if len(a) != len(b) {
        return 0, errors.New("strings should be of equal length")
    }

    d := 0
    for i := 0; i < len(a); i++ {
        if a[i] != b[i] {
            d++
        }
    }
    return d, nil
}

您应该定义置信度(min_confidence 参数)。

例如:golint -min_confidence 0 your_file_to_test

查看golint用法:

golint -h
Usage of golint:
    golint [flags] # runs on package in current directory
    golint [flags] [packages]
    golint [flags] [directories] # where a '/...' suffix includes all sub-directories
    golint [flags] [files] # all must belong to a single package
Flags:
  -min_confidence float
        minimum confidence of a problem to print it (default 0.8)
  -set_exit_status
        set exit status to 1 if any issues are found