github.com/songzhibin97/gkit@v1.2.13/ternary/type_gen.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package main 5 6 import ( 7 "bytes" 8 "go/format" 9 "io/ioutil" 10 "os" 11 "strings" 12 ) 13 14 var packageName = "package ternary" 15 16 func main() { 17 f, err := os.Open("template.txt") 18 if err != nil { 19 panic(err) 20 } 21 fileData, err := ioutil.ReadAll(f) 22 if err != nil { 23 panic(err) 24 } 25 26 w := new(bytes.Buffer) 27 start_pos := strings.Index(string(fileData), packageName) 28 w.WriteString(string(fileData)[start_pos : start_pos+len(packageName)]) 29 30 ts := []string{"Bool", "Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Int64", "Rune", "String", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"} 31 32 for _, upper := range ts { 33 lower := strings.ToLower(upper) 34 data := string(fileData) 35 36 data = data[start_pos+len(packageName):] 37 38 data = strings.Replace(data, "{{upper}}", upper, -1) 39 data = strings.Replace(data, "{{lower}}", lower, -1) 40 41 w.WriteString(data) 42 w.WriteString("\r\n") 43 } 44 45 comps := []struct { 46 upper string 47 lower string 48 }{ 49 {"Interface", "interface{}"}, 50 {"Time", "time.Time"}, 51 {"Duration", "time.Duration"}, 52 } 53 54 for _, comp := range comps { 55 56 data := string(fileData) 57 58 data = data[start_pos+len(packageName):] 59 60 data = strings.Replace(data, "{{upper}}", comp.upper, -1) 61 data = strings.Replace(data, "{{lower}}", comp.lower, -1) 62 63 w.WriteString(data) 64 w.WriteString("\r\n") 65 } 66 67 out, err := format.Source(w.Bytes()) 68 if err != nil { 69 panic(err) 70 } 71 if err := ioutil.WriteFile("types.go", out, 0660); err != nil { 72 panic(err) 73 } 74 }