我正在传递一个uuid使用Context和WithValue处理它的后续函数*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)
什么是可用于检索此键的最佳选项,该键不是像简单字符串那样的基本类型?
我已经在 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 错误SayHello是undeclared name:
我在这里(https://github.com/golang/lint/issues/57)读到了一个类似的问题,但由于这个问题已经五年了,我认为它现在可能已经解决了?或者golint根本不能跨多个文件工作?
我收到此错误消息:
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。
运行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) 我收到以下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) 我按照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 吗?
我尝试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 中跳过文件/包?
我有一个使用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中,我们经常用ifstatement和.声明来编写代码return err。像这样:
if res, err := getResult(); err != nil {\n return err\n } else {\n fmt.Println(res)\n // do something with res\n }\nRun Code Online (Sandbox Code Playgroud)\n但 linter 总是告诉我应该else在之后删除块return:
\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)\nRun 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) go-lint 建议如下:
method CreateStaticCssPath should be CreateStaticCSSPath
Run Code Online (Sandbox Code Playgroud)
linter 是否正确,如果正确,为什么?
它允许以前的方法:
CreateStaticJsPath
Run Code Online (Sandbox Code Playgroud)