gpt4 book ai didi

golint 执行不返回任何内容

转载 作者:行者123 更新时间:2023-12-01 22:34:58 25 4
gpt4 key购买 nike

我正在为exercism.io 编写练习。我的导师说我的代码应该由一些 go lint 来检查,他建议我尝试 golint 和 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
}

还有我的 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

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

最佳答案

在 Go 中,最少的检查可能是 Go 工具 go fmt , go vet , 和 golint .

这是一个最小的、可重复的示例(参见 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 并不完美,同时存在误报和误报。不要将其输出视为黄金标准。”

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
}

关于golint 执行不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com