本文整理汇总了Golang中go/types.Config.DisableUnusedImportCheck方法的典型用法代码示例。如果您正苦于以下问题:Golang Config.DisableUnusedImportCheck方法的具体用法?Golang Config.DisableUnusedImportCheck怎么用?Golang Config.DisableUnusedImportCheck使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Config
的用法示例。
在下文中一共展示了Config.DisableUnusedImportCheck方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: do
//do useful for testing
func do(writer io.Writer, flags *flag.FlagSet, args []string) error {
var pkgPath string
var rematchUser string
flags.Usage = usage
writeToFilesFlag := flags.Bool("w", false, "write to files")
simulateFlag := flags.Bool("s", false, "simulate updates")
flags.Parse(args)
pkgPath, rematchUser = parseArgs(flags)
packages, err := parser.ParseDir(fileSet, pkgPath, isGoFile, 0)
if err != nil {
return fmt.Errorf("parserDir: %s", err.Error())
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
var files []*ast.File
fileByPath := make(map[string]*os.File) //cache files
for _, pkg := range packages {
for filePath, file := range pkg.Files {
fp, err := os.OpenFile(filePath, os.O_RDWR, 0777)
if err != nil {
return err
}
defer fp.Close()
files = append(files, file)
fileByPath[filePath] = fp
}
}
//parse package
var conf types.Config
conf.Importer = importer.Default()
conf.DisableUnusedImportCheck = true
conf.Error = func(err error) {
//omit errors try parse it
}
pkg, _ := conf.Check(pkgPath, fileSet, files, &info)
//queries for user matching
queries := make(chan *queryIdent)
//queries for unexported fields used it for detect collision
chqueriesUnexported := make(chan *queryIdent)
go buildQueryStrings(queries, pkg, &info, true)
go buildQueryStrings(chqueriesUnexported, pkg, &info, false)
queriesUnexported := make(map[string]*ast.Ident)
for queryUnexport := range chqueriesUnexported {
queriesUnexported[queryUnexport.Query] = queryUnexport.Ident
}
//go routine for unexporting
doneSave := make(chan bool)
unexportPositions := make(chan token.Position, 1)
unexports := make(map[string]bool)
//start updater on files
go unexportOnFile(doneSave, fileByPath, unexportPositions, *simulateFlag)
//try match user query
var where func(string) bool
if rematchUser == "" {
rematchUser = ".+"
}
where = regexp.MustCompile(rematchUser).MatchString
//used only for showing
var generalQueries []*queryIdent
for query := range queries {
if where(query.Query) {
//detect collision
_, isKeyword := goKeywords[nameUnexported(query.Ident.Name)]
if queriesUnexported[query.Query] != nil || isKeyword {
if !*writeToFilesFlag {
generalQueries = append(generalQueries, &queryIdent{query.Query + " !!collision", query.Ident})
} else {
fmt.Fprintln(writer, "sorry collision detected for", query.Query)
}
continue
}
pos := fileSet.Position(query.Ident.Pos())
unexports[query.Ident.Name] = true
if !*writeToFilesFlag {
generalQueries = append(generalQueries, query)
continue
}
fmt.Fprintln(writer, "Unexported", query.Ident.Name, "from", query.Query)
unexportPositions <- pos
}
//.........这里部分代码省略.........