-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
51 lines (46 loc) · 859 Bytes
/
Copy pathmain_test.go
File metadata and controls
51 lines (46 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Parser: How to parse comments from Go Code
//
// Comments from Go code can be parsed using
// go/parser package
package main
import (
"fmt"
"go/parser"
"go/token"
)
func Example() {
src := `
// Calculator package provides methods
// for basic int calculation
package calculator
// Import of fmt package
import "fmt"
// Add adds two integers
func Add(a, b int) int {
// calculate the result
result := a + b
// return the result
return result
}
`
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "", src, parser.ParseComments)
if err != nil {
fmt.Println(err)
return
}
for _, c := range f.Comments {
fmt.Println(c.Text())
}
// Output:
// Calculator package provides methods
// for basic int calculation
//
// Import of fmt package
//
// Add adds two integers
//
// calculate the result
//
// return the result
}