Golang go/ast.FileExports() function example
package go/ast
FileExports trims the AST for a Go source file in place such that only exported nodes remain: all top-level identifiers which are not exported and their associated information (such as type, initial value, or function body) are removed. Non-exported fields and methods of exported types are stripped. The File.Comments list is not changed.
FileExports returns true if there are exported declarations; it returns false otherwise.
Golang ast.FileExports() function usage example
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
var fset = token.NewFileSet()
func main() {
f, err := parser.ParseFile(fset, "", `
package p
type T struct {
/* F1 lead comment */
//
F1 int /* F1 */ // line comment
// F2 lead
// comment
F2 int // F2 line comment
// f3 lead comment
f3 int // f3 line comment
}
`, parser.ParseComments)
if err != nil {
fmt.Println(err)
}
fmt.Println(ast.FileExports(f)) // true if there are exported declarations
fmt.Println(f) // show the trimmed version of f
}
Output :
true
&{
2 p [0x2082421c0] scope 0x2082101f0 { type T
}
[] [int int int] [0x2082560c0 0x208256160 0x2082561c0 0x2082562a0 0x2082562e0 0x208256380]}
Reference :
Advertisement
Something interesting
Tutorials
+34k Golang : Proper way to set function argument default value
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+7k Golang : How to call function inside template with template.FuncMap
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+15.2k Golang : Save(pipe) HTTP response into a file
+17.4k Golang : Check if IP address is version 4 or 6
+20.9k PHP : Convert(cast) int to double/float
+29.5k Golang : Saving(serializing) and reading file with GOB
+36.3k Golang : Convert(cast) int64 to string
+25.7k Golang : How to write CSV data to file
+17k Golang : Get input from keyboard
+7.1k Golang : A simple forex opportunities scanner