标签: golint

修复"不应该在context.WithValue中使用基本类型字符串作为键"golint

我正在传递一个uuid使用ContextWithValue处理它的后续函数*http.request.此uuid已在授权标头中传递给REST调用以标识某个人.验证授权令牌并且需要可访问以检查呼叫本身是否已被授权.

我用了:

ctx := context.WithValue(r.Context(), string("principal_id"), *id)
Run Code Online (Sandbox Code Playgroud)

但是高尔特抱怨道:

should not use basic type string as key in context.WithValue
Run Code Online (Sandbox Code Playgroud)

什么是可用于检索此键的最佳选项,该键不是像简单字符串那样的基本类型?

go golint

33
推荐指数
3
解决办法
9856
查看次数

在 VS 代码中进行短绒检查不适用于跨多个文件的包?

我已经在 MacOS 上的 Visual Studio Code 中安装了 Go 扩展(版本 0.11.4):

在此处输入图片说明

但是,我发现 linter 不会“拾取”在同一个包中定义的函数,而是在不同的文件中。例如,如果我在同一个目录中的文件创建foo.go

package foobar

import "fmt"

func main() {
    fmt.Println(SayHello())
}
Run Code Online (Sandbox Code Playgroud)

和文件bar.go

package foobar

func SayHello() string {
    return "Hello, world!"
}
Run Code Online (Sandbox Code Playgroud)

然后在foo.go我收到一个 linter 错误SayHelloundeclared name

在此处输入图片说明

我在这里(https://github.com/golang/lint/issues/57)读到了一个类似的问题,但由于这个问题已经五年了,我认为它现在可能已经解决了?或者golint根本不能跨多个文件工作?

go golint visual-studio-code

18
推荐指数
5
解决办法
1万
查看次数

“文件不是 `gci`-ed with --skip- generated -s standard,default (gci)”的含义

我收到此错误消息:

main.go:24: File is not `gci`-ed with --skip-generated -s standard,default (gci)
import (
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

背景:我是 Go 新手,并且 linting 不是我设置的。我承认我不知道产生此警告的实际 linter。

go golint

18
推荐指数
2
解决办法
8160
查看次数

如何在范围上使用指针或索引来解析 rangeValCopy gocritic 消息

运行https://golangci-lint.run/时得到以下输出:

rangeValCopy: each iteration copies 128 bytes (consider pointers or indexing) (gocritic)
    for _, v := range products {
Run Code Online (Sandbox Code Playgroud)

这是我正在运行的代码的精简版本:

package main

import (
    "fmt"
    "encoding/json"
)

type Application struct {
    ProductData   []ProductDatum
}

type ProductDatum struct {
    Name          string
    ProductBrand  string
    ProductType   string
}

type Item struct {
    ProductBrand string
    ProductName  string
    ProductType  string
}

func main() {
    appl := Application{
        ProductData: []ProductDatum{
            {
                Name:         "Baz",
                ProductBrand: "Foo",
                ProductType:  "Bar",
            },
        },
    }

    products := appl.ProductData

    var orderLinesItem …
Run Code Online (Sandbox Code Playgroud)

go golint

6
推荐指数
1
解决办法
6283
查看次数

G110:通过减压炸弹 (gosec) 的潜在 DoS 漏洞

我收到以下golintci消息:

testdrive/utils.go:92:16: G110: Potential DoS vulnerability via decompression bomb (gosec)
    if _, err := io.Copy(targetFile, fileReader); err != nil {
                 ^
Run Code Online (Sandbox Code Playgroud)

阅读相应的CWE,我不清楚如何纠正这个问题。

请各位指点。

func unzip(archive, target string) error {
    reader, err := zip.OpenReader(archive)
    if err != nil {
        return err
    }

    for _, file := range reader.File {
        path := filepath.Join(target, file.Name) // nolint: gosec
        if file.FileInfo().IsDir() {
            if err := os.MkdirAll(path, file.Mode()); err != nil {
                return err
            }
            continue
        }

        fileReader, err := file.Open() …
Run Code Online (Sandbox Code Playgroud)

static-analysis go golint

6
推荐指数
2
解决办法
3055
查看次数

golangci-lint 重新安装后出现看不见的错误

我按照https://go.dev/doc/installgo上的说明将 的版本升级到 go1.18.3 :

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
Run Code Online (Sandbox Code Playgroud)

然后我尝试为我的go项目运行Makefile,但提示未安装golangci-lint。我认为这是由于rm -rf /usr/local/go所有软件包都被删除了,或者我在升级过程中以某种方式弄乱了文件。我继续安装 golangci-lint:

go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.46.2
Run Code Online (Sandbox Code Playgroud)

报告了许多以前未见过的错误。喜欢

could not import math/bits (-: could not load export data: cannot import "math/bits" (unknown iexport format version 2), export data is newer version - update tool)'

undeclared name: `yaml

...
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为该项目过去在 lint 检查后编译成功。我应该降级 gplangci-lint 吗?

go golint golangci-lint

6
推荐指数
1
解决办法
2080
查看次数

如何在 golangci-lint 中跳过文件?

我尝试go1.16

import "embed"
Run Code Online (Sandbox Code Playgroud)

我明白了

> golangci-lint run ./...
> Can't run linter goanalysis_metalinter: bodyclose: failed prerequisites ... could not import embed
Run Code Online (Sandbox Code Playgroud)

如何在 golangci-lint 中跳过文件/包?

go golint

5
推荐指数
1
解决办法
1万
查看次数

无法安装golint软件包:错误的导入路径

我有一个使用golint的项目并将其安装在docker容器中。它已经工作了好几个月(并且已经构建了多次),但是今天当我再次构建容器时,出现以下错误:go -u github.com/golang/lint/golint

package github.com/golang/lint/golint: 
code in directory /a-go-path/golang/lint/golint expects import "golang.org/x/lint/golint"
Run Code Online (Sandbox Code Playgroud)

我可以通过输入go get github.com/golang/lint/golint在本地计算机上复制该问题。我已经删除了go路径(源文件夹和bin文件夹)中与golint相关的所有软件包,但是当我再次尝试安装它时,仍然收到前一个错误。我正在使用go 1.11.0有什么建议吗?

go docker golint

1
推荐指数
1
解决办法
1044
查看次数

应该将 return err 放在带声明的 if 语句的 else 中,还是避免这种风格并减少 Golang 的返回值?

在Go中,我们经常用ifstatement和.声明来编写代码return err。像这样:

\n
    if res, err := getResult(); err != nil {\n        return err\n    } else {\n        fmt.Println(res)\n        // do something with res\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

但 linter 总是告诉我应该else在之后删除块return

\n
  \xe2\x9a\xa0  https://revive.run/r#indent-error-flow  if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)\n
Run Code Online (Sandbox Code Playgroud)\n

代码片段应如下所示以满足建议:

\n
  \xe2\x9a\xa0  https://revive.run/r#indent-error-flow  if block ends with a return statement, so drop this else …
Run Code Online (Sandbox Code Playgroud)

if-statement lint declaration go golint

1
推荐指数
1
解决办法
2354
查看次数

为什么 go-lint 会给出关于首字母缩写的不一致建议?

go-lint 建议如下:

method CreateStaticCssPath should be CreateStaticCSSPath
Run Code Online (Sandbox Code Playgroud)

linter 是否正确,如果正确,为什么?

它允许以前的方法:

CreateStaticJsPath
Run Code Online (Sandbox Code Playgroud)

go golint

-2
推荐指数
1
解决办法
58
查看次数