import ( "k8s.io/kubernetes/cmd/libs/go2idl/generator" "os" ) func main() { sw := generator.NewSnippetWriter(os.Stdout, "//", "\n") sw.WriteSnippet("type {{.Name}} struct {\n{{range .Fields}}{{.Name}} {{.Type}} {{.Tag}}\n{{end}}}") }
import ( "k8s.io/kubernetes/cmd/libs/go2idl/generator" "os" ) func main() { sw := generator.NewSnippetWriter(os.Stdout, "//", "\n") for _, param := range params { sw.WriteMultipleSnippets(` func ({{.Receiver}} *{{.StructName}}) {{.MethodName}}({{.ParamName}} {{.ParamType}}) {{.ReturnType}} { return {{.Receiver}}.{{.FieldName}} + {{.ParamName}} } `, map[string]interface{}{ "Receiver": "t", "StructName": "MyType", "MethodName": "MyMethod", "ParamName": param.Name, "ParamType": param.Type, "ReturnType": param.Return, "FieldName": param.Field, }) } }This code creates a new SnippetWriter with os.Stdout as the output writer and uses the WriteMultipleSnippets method to generate multiple code snippets for a method that takes a parameter, adds it to a field value, and returns the result. The data for each snippet is provided in a map that includes the receiver, struct name, method name, parameter name, parameter type, return type, and field name.