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
+13k Golang : Get terminal width and height example
+8.5k Golang : How to check variable or object type during runtime?
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+8.2k Golang : Routes multiplexer routing example with regular expression control
+7.4k Android Studio : How to detect camera, activate and capture example
+37.5k Golang : Converting a negative number to positive number
+25.3k Golang : Get current file path of a file or executable
+35.5k Golang : Smarter Error Handling with strings.Contains()
+7.9k Golang : Grayscale Image
+8.1k Golang : Randomize letters from a string example
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+13.5k Golang : How to get year, month and day?