死刑执行不返回任何内容

时间:2019-12-25 11:28:42

标签: go golint

我正在为exercism.io编码练习。我的导师说,我的代码应该由一些短绒棉衣检查,他建议我尝试使用棉绒和golangci-lint。 我通过go get -u golang.org/x/lint/golint安装了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
}

还有我去的环境:

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

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

2 个答案:

答案 0 :(得分:1)

在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
}
$

  

评论:我的导师发送了他的短毛绒处决,并且有一些消息,例如

$ 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并不完美,同时具有误报和误报。不要将其输出视为黄金标准。”

  

Golint is a linter for Go source code.

     

目的

     

Golint与gofmt不同。 Gofmt重新格式化Go源代码,而   golint打印出样式错误。

     

戈林特不同于高丝。高维(Govet)关注正确性,   而golint与编码风格有关。 Golint正在使用中   Google,并力求与开源Go的风格相匹配   项目。

     

golint提出的建议正好是:建议。戈林特   是不完美的,具有误报和误报。做   不将其输出视为黄金标准。我们不会增加实用性   或其他旋钮以禁止显示特定警告,因此不要指望或   要求代码完全“无毛绒”。简而言之,该工具不是,   并且永远不会足够值得其建议   自动执行,例如作为构建过程的一部分。戈林特   为列出的许多机械检查项目提供建议   在Effective Go和CodeReviewComments Wiki页面中。


这是我尝试解决的方法:

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
}

答案 1 :(得分:1)

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

例如:golint -min_confidence 0 your_file_to_test

查看脚垫的用法:

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
相关问题