Golang go/printer.CommentedNode type example
package go/printer
A CommentedNode bundles an AST node and corresponding comments. It may be provided as argument to any of the Fprint functions.
Golang go/printer.CommentedNode type usage example
func (lp *linePrinter) printWithComments(n ast.Node) {
nfirst := lp.fset.Position(n.Pos()).Line
nlast := lp.fset.Position(n.End()).Line
for _, g := range lp.fnode.Comments {
cfirst := lp.fset.Position(g.Pos()).Line
clast := lp.fset.Position(g.End()).Line
if clast == nfirst-1 && lp.fset.Position(n.Pos()).Column == lp.fset.Position(g.Pos()).Column {
for _, c := range g.List {
lp.output.WriteString(c.Text)
lp.output.WriteByte('\n')
}
}
if cfirst >= nfirst && cfirst <= nlast && n.End() <= g.List[0].Slash {
// The printer will not include the comment if it starts past
// the node itself. Trick it into printing by overlapping the
// slash with the end of the statement.
g.List[0].Slash = n.End() - 1
}
}
node := &printer.CommentedNode{n, lp.fnode.Comments} // <-- here
lp.config.Fprint(&lp.output, lp.fset, node)
}
References :
Advertisement
Something interesting
Tutorials
+9.7k Golang : Populate slice with sequential integers example
+11.1k Golang : Simple image viewer with Go-GTK
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+8.4k Your page has meta tags in the body instead of the head
+6.2k Golang : Process non-XML/JSON formatted ASCII text file example
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+5k Python : Convert(cast) bytes to string example
+8k Golang : What fmt.Println() can do and println() cannot do
+7.7k Golang : Generate human readable password
+13.2k Golang : Convert(cast) int to int64