Skip to content

Instantly share code, notes, and snippets.

@ikawaha
Created February 5, 2015 02:22
Show Gist options
  • Save ikawaha/77777c2b0615ee62d751 to your computer and use it in GitHub Desktop.
Save ikawaha/77777c2b0615ee62d751 to your computer and use it in GitHub Desktop.
show goimports
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
)
func main() {
for i, arg := range os.Args {
if i == 0 {
continue
}
fmt.Println(arg)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, arg, nil, 0)
if err != nil {
panic(err)
}
for _, decl := range f.Decls {
switch td := decl.(type) {
case *ast.GenDecl:
switch td.Tok {
case token.IMPORT:
for _, sp := range td.Specs {
s := sp.(*ast.ImportSpec)
fmt.Println(s.Path.Value)
}
default:
}
}
}
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment