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
+9.7k PHP : Get coordinates latitude/longitude from string
+10.5k Golang : Create matrix with Gonum Matrix package example
+51.4k Golang : Check if item is in slice/array
+11.1k Golang : Read until certain character to break for loop
+11.1k Golang : Fix go.exe is not compatible with the version of Windows you're running
+10.1k Golang : Check a web page existence with HEAD request example
+5k Golang : Display packages names during compilation
+6.1k Golang : Measure execution time for a function
+17k Golang : XML to JSON example
+8k Golang : What fmt.Println() can do and println() cannot do
+10.4k Golang : Generate random integer or float number