10 examples of 'go generate' in Go

Every line of 'go generate' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
174func (g *Graph) Gen() error {
175 var gen Generator = GenerateFunc(generate)
176 for i := len(g.Hooks) - 1; i >= 0; i-- {
177 gen = g.Hooks[i](gen)
178 }
179 return gen.Generate(g)
180}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

97func doGen(g string) bool {
98 g = strings.Replace(g, "-gen", "", -1)
99 return generators.Has(g) || generators.Len() == 0
100}
72func (gl *goLibrary) generate() error {
73 // generate all Type structs
74 if err := generateStructs(gl.Types, gl.targetDir()); err != nil {
75 return err
76 }
77
78 // security schemes
79 if err := generateSecurity(gl.SecuritySchemes, gl.targetDir(), gl.PackageName); err != nil {
80 return err
81 }
82
83 // included libraries
84 for name, ramlLib := range gl.Libraries {
85 childLib := newGoLibrary(name, ramlLib, gl.targetDir(), gl.rootTargetDir, globLibRootURLs)
86 if err := childLib.generate(); err != nil {
87 return err
88 }
89 }
90 return nil
91}
24func Generate() (files []string, err error) {
25 var outDir, ver string
26 set := flag.NewFlagSet("swagger", flag.PanicOnError)
27 set.StringVar(&outDir, "out", "", "")
28 set.StringVar(&ver, "version", "", "")
29 set.String("design", "", "")
30 set.Parse(os.Args[1:])
31
32 if err := codegen.CheckVersion(ver); err != nil {
33 return nil, err
34 }
35
36 g := &Generator{OutDir: outDir, API: design.Design}
37
38 return g.Generate()
39}
16func (d *derivedGen) Generate(genParams *GenParameters) *GenResult {
17 labels := []string{}
18 up := make([]interface{}, len(d.upGens))
19 shrinkers := make([]Shrinker, len(d.upGens))
20 sieves := make([]func(v interface{}) bool, len(d.upGens))
21
22 var ok bool
23 for i, gen := range d.upGens {
24 result := gen(genParams)
25 labels = append(labels, result.Labels...)
26 shrinkers[i] = result.Shrinker
27 sieves[i] = result.Sieve
28 up[i], ok = result.Retrieve()
29 if !ok {
30 return &GenResult{
31 Shrinker: d.Shrinker,
32 Result: nil,
33 Labels: result.Labels,
34 ResultType: d.resultType,
35 Sieve: d.Sieve,
36 }
37 }
38 }
39 down := d.biMapper.ConvertDown(up)
40 if len(down) == 1 {
41 return &GenResult{
42 Shrinker: d.Shrinker,
43 Result: down[0],
44 Labels: labels,
45 ResultType: reflect.TypeOf(down[0]),
46 Sieve: d.Sieve,
47 }
48 }
49 return &GenResult{
50 Shrinker: d.Shrinker,
51 Result: down,
52 Labels: labels,
53 ResultType: reflect.TypeOf(down),
54 Sieve: d.Sieve,
55 }
56}
248func (p CustomNameType) Generate(out *Emitter) {
249 out.Print(p.Type)
250}
45func gen(arch string, tags, zero, copy func(io.Writer)) {
46 var buf bytes.Buffer
47
48 fmt.Fprintln(&buf, "// Code generated by mkduff.go; DO NOT EDIT.")
49 fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
50 fmt.Fprintln(&buf, "// See mkduff.go for comments.")
51 tags(&buf)
52 fmt.Fprintln(&buf, "#include \"textflag.h\"")
53 fmt.Fprintln(&buf)
54 zero(&buf)
55 fmt.Fprintln(&buf)
56 copy(&buf)
57
58 if err := os.WriteFile("duff_"+arch+".s", buf.Bytes(), 0644); err != nil {
59 log.Fatalln(err)
60 }
61}
56func (g generatorWrapper) Generate(_ *cobra.Command, _ []string) error {
57 start := time.Now()
58
59 if err := g.gf(basePath, ignoreRegex).Generate(isos); err != nil {
60 log.Println("generation aborted:", err.Error())
61 return err
62 }
63
64 log.Println("site generated! time:", time.Since(start))
65 return nil
66}
19func Generate(opts GenOpts) bool {
20 bs, ok := language_map[opts.Language]
21 if !ok {
22 log.Println("Support for language ", opts.Language, " not implemented yet")
23 }
24 return bs.Generate(shared.GenOpts(opts))
25}
16func Generate(name string, g generator) (updated bool, err error) {
17 target := fmt.Sprintf("%s/%s.go", typenv.AppMainPath, name)
18 if updated, err = metadata.Update(name, g); err != nil {
19 return
20 }
21 if updated = updated || !filekit.IsExist(target); updated {
22 err = g.generate(target)
23 }
24 return
25}

Related snippets