genr generates source code to emulate generic type
.
package main
import (
"github.com/openacid/genr"
)
var implHead = `package intarray
import (
"encoding/binary"
)
`
var implTemplate = `
type {{.Name}} struct {
vals []{{.ValType}}
eltSize int
first []byte
}
func New{{.Name}}(elts []{{.ValType}}) (a *{{.Name}}, err error) {
a = &{{.Name}}{
vals: make([]{{.ValType}}, 10),
eltSize: {{.ValLen}},
first: make([]byte, 0),
}
binary.LittleEndian.Put{{.Codec}}(a.first, {{.EncodeCast}}(elts[0]))
return a, nil
}
`
func main() {
implfn := "../intarray.go"
impls := []interface{}{
genr.NewIntConfig("U16", "uint16"),
genr.NewIntConfig("I64", "int64"),
}
genr.Render(implfn, implHead, implTemplate, impls, []string{"gofmt", "unconvert"})
}
The generated codes looks like the following:
// Code generated 'by go generate ./...'; DO NOT EDIT.
package intarray
import (
"encoding/binary"
)
type U16 struct {
vals []uint16
eltSize int
first []byte
}
func NewU16(elts []uint16) (a *U16, err error) {
a = &U16{
vals: make([]uint16, 10),
eltSize: 2,
first: make([]byte, 0),
}
binary.LittleEndian.PutUint16(a.first, elts[0])
return a, nil
}
type I64 struct {
vals []int64
eltSize int
first []byte
}
func NewI64(elts []int64) (a *I64, err error) {
a = &I64{
vals: make([]int64, 10),
eltSize: 8,
first: make([]byte, 0),
}
binary.LittleEndian.PutUint64(a.first, uint64(elts[0]))
return a, nil
}