hhstore / blog

My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
296 stars 24 forks source link

Golang: go:generate - Generating code #175

Open hhstore opened 5 years ago

hhstore commented 5 years ago

related:

hhstore commented 5 years ago

golang 代码生成器工具: go:generate

ref:

应用场景:

应用范例:

官方:

项目级别:

hhstore commented 5 years ago

go 标准库: ast/parser/token

ref:


package gen

import (
    "go/ast"
    "go/parser"
    "go/token"
)

// This example shows what an AST looks like when printed for debugging.
func ExamplePrint() {
    // src is the input for which we want to print the AST.
    src := `
package main

// main: this is main func
func main() {
    // call println -key1=value1 -key2=value2
    println("Hello, World!")
}
`

    // Create the AST by parsing src.
    fset := token.NewFileSet()                                      // positions are relative to fset
    f, err := parser.ParseFile(fset, "", src, parser.ParseComments) // [mode=0]
    if err != nil {
        panic(err)
    }

    // Print the AST.
    ast.Print(fset, f)
}

package gen

import (
    "testing"
)

func TestExamplePrint(t *testing.T) {
    ExamplePrint()
}
hhstore commented 5 years ago

go 标准库: template

crud 定义: