Staticcheck 是一个开源的 Go 语言静态代码分析工具。
例如用来分析无用的变量:
package main import ( "errors" "fmt" "log" ) type Result struct { Entries []string } func Query() (Result, error) { return Result{ Entries: []string{}, }, nil } func ResultEntries() (Result, error) { err := errors.New("no entries found") result, err := Query() if err != nil { return Result{}, err } if len(result.Entries) == 0 { return Result{}, err } return result, nil } func main() { result, err := ResultEntries() if err != nil { log.Fatal(err) } fmt.Printf("result=%v, err=%v", result, err) }
分析结果:
$ staticcheck main.go main.go:20:2: this value of err is never used (SA4006) main.go:20:19: New is a pure function but its return value is ignored (SA4017)