github.com/songzhibin97/gkit@v1.2.13/tools/pointer/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 pointer"
    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{"Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Int64", "Rune", "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  	out, err := format.Source(w.Bytes())
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	if err := ioutil.WriteFile("types.go", out, 0660); err != nil {
    50  		panic(err)
    51  	}
    52  }