github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/hammers/codegen.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"os"
     7  	"text/template"
     8  )
     9  
    10  func main() {
    11  	const stub = "func ({{.Recv}}) {{.Name}}" +
    12  		"({{range .Params}}{{.Name}} {{.Type}}, {{end}})" +
    13  		"({{range .Res}}{{.Name}} {{.Type}}, {{end}})" +
    14  		"{\n}\n\n"
    15  	tmpl := template.Must(template.New("test").Parse(stub))
    16  
    17  	m := Method{
    18  		Recv: "f *File",
    19  		Func: Func{
    20  			Name: "Close",
    21  			Res:  []Param{{Type: "error"}},
    22  		},
    23  	}
    24  
    25  	tmpl.Execute(os.Stdout, m)
    26  }
    27  
    28  // Method represents a method signature.
    29  type Method struct {
    30  	Recv string
    31  	Func
    32  }
    33  
    34  // Func represents a function signature.
    35  type Func struct {
    36  	Name   string
    37  	Params []Param
    38  	Res    []Param
    39  }
    40  
    41  // Param represents a parameter in a function or method signature.
    42  type Param struct {
    43  	Name string
    44  	Type string
    45  }