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
+32.7k Golang : Regular Expression for alphanumeric and underscore
+5.6k Swift : Get substring with rangeOfString() function example
+12.7k Golang : Sort and reverse sort a slice of bytes
+8.3k Golang : Number guessing game with user input verification example
+6.9k Fix sudo yum hang problem with no output or error messages
+9.9k Golang : Turn string or text file into slice example
+15.2k Golang : Accurate and reliable decimal calculations
+7.8k Swift : Convert (cast) String to Double
+8.3k Golang : Implementing class(object-oriented programming style)
+10.1k Golang : Print how to use flag for your application example
+12.3k Golang : 2 dimensional array example
+20.3k Swift : Convert (cast) Int to int32 or Uint32