1 Star 0 Fork 0

goproxies/github.com-alecthomas-gometalinter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
check_no_init.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
func checkNoInits(rootPath string) ([]string, error) {
const recursiveSuffix = string(filepath.Separator) + "..."
recursive := false
if strings.HasSuffix(rootPath, recursiveSuffix) {
recursive = true
rootPath = rootPath[:len(rootPath)-len(recursiveSuffix)]
}
messages := []string{}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if !recursive && path != rootPath {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return err
}
for _, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
filename := fset.Position(funcDecl.Pos()).Filename
line := fset.Position(funcDecl.Pos()).Line
name := funcDecl.Name.Name
if name == "init" && funcDecl.Recv.NumFields() == 0 {
message := fmt.Sprintf("%s:%d %s function", filename, line, name)
messages = append(messages, message)
}
}
return nil
})
return messages, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/goproxies/github.com-alecthomas-gometalinter.git
git@gitee.com:goproxies/github.com-alecthomas-gometalinter.git
goproxies
github.com-alecthomas-gometalinter
github.com-alecthomas-gometalinter
v2.0.12

搜索帮助

0d507c66 1850385 C8b1a773 1850385