-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgenfile.go
More file actions
46 lines (36 loc) · 654 Bytes
/
genfile.go
File metadata and controls
46 lines (36 loc) · 654 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
package astextract
import (
"go/format"
"io/ioutil"
"os"
"path/filepath"
)
func genFile(outfile string, out string) error {
y := `package main
import (
"go/ast"
"go/printer"
"go/token"
"log"
"os"
)
func main() {
z := ` + out + `
// example usage
f, err := os.OpenFile("gen.go", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Fatalln(err)
}
fSet := token.NewFileSet()
printer.Fprint(f, fSet, z)
}`
err := os.MkdirAll(filepath.Dir(outfile), 0755)
if err != nil {
return err
}
formatted, err := format.Source([]byte(y))
if err != nil {
return err
}
return ioutil.WriteFile(outfile, formatted, 0644)
}