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
+10.6k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+25.7k Golang : How to write CSV data to file
+5.9k AWS S3 : Prevent Hotlinking policy
+19.3k Golang : Get RGBA values of each image pixel
+12.1k Golang : Decompress zlib file example
+6k Golang : Compound interest over time example
+9k Golang : Inject/embed Javascript before sending out to browser example
+11.2k Golang : How to pipe input data to executing child process?
+8.1k Golang : Randomize letters from a string example
+19.2k Golang : Execute shell command