Yet Another Go Generic Generator
There are a few options for statically generating generics for Go. I mention them in the Alternatives section. However, I was interested in the new go generate in combination with the template library. It currently only supports slices and maps and only using the built in templates that I wrote. Future versions will have support for external templates.
Feel free to fork or pull.
Vary much a work in progress but functional and useful.
go get github.com/dshills/yaggg
User Type must implement 3 functions Cmp, Clone, Clear in the following forms:
func (a MyType)Cmp(b MyType) int Cmp must return 0 for equality, 1 when a > b, -1 when a < b
func (a MyType)Clone() MyType Clone must return a deep copy of MyType
func (a MyType)Clear() MyType Clear must return a valid empty MyType
yaggg --output <outfile> --generate <map | slice> --container <conName> --value <mytype> --key <mapKey>
example:
yagg --output mytypemap.go --generate map --container mytypemap --value mytype --key string
Using Go Generate
//go:generate yagg --output mytypemap.go --generate map --container mytypemap --value mytype --key string
package main
//go:generate yaggg --output boguses.go --container boguses --value bogus
//go:generate yaggg --output bogusmap.go --container bogusmap --value bogus --key string --generate map
type bogus struct {
ID int
Bool bool
Int int
Float float64
Slice []string
}
func (b bogus) Cmp(j bogus) int {
if b.ID == j.ID {
return 0
}
if b.ID > j.ID {
return 1
}
return -1
}
func (b bogus) Clone() bogus {
c := b
return c
}
func (b bogus) Clear() bogus {
return bogus{}
}
func main() {
}
Almost everything
Copyright 2015 Davin Hills. All rights reserved. MIT license. License details can be found in the LICENSE file.
Version | Tag | Published |
---|---|---|
v0.0.0-20150413121914-564c600c1d8d | 1yr ago |