Golang go/ast.SortImports() function example
package go/ast
SortImports sorts runs of consecutive import lines in import blocks in f. It also removes duplicate imports when it is possible to do so without data loss.
Golang go/ast.SortImports() usage example
var pkg string
...
buf := new(bytes.Buffer)
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, pkg+".go", buf, parser.ParseComments)
if err != nil {
return errors.New("Error parsing generated code: " + err.Error())
}
// Sort the import lines in the AST in the same way that gofmt does.
ast.SortImports(fset, astFile)
References :
http://golang.org/pkg/go/ast/#SortImports
https://github.com/jacobsa/oglemock/blob/master/generate/generate.go
Advertisement
Something interesting
Tutorials
+12.2k Golang : Split strings into command line arguments
+34.1k Golang : Create x509 certificate, private and public keys
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+17.2k Golang : When to use init() function?
+16.1k Golang : Generate universally unique identifier(UUID) example
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.8k Golang : Reverse a string with unicode
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+39.6k Golang : Remove dashes(or any character) from string
+7.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+17.8k Golang : Defer function inside init()