I need a data structure which accepts name / value pairs in addition to custom fields. How can I define a such structure ?
e.g.
type mybasket struct {
Coupons string
Amount int
....... // string or int
}
I need a data structure which accepts name / value pairs in addition to custom fields. How can I define a such structure ?
e.g.
type mybasket struct {
Coupons string
Amount int
....... // string or int
}
I'd recommend defining setter
and getter
methods on the type, and store the values in a slice in the struct.
For example:
package main
import "fmt"
type kv struct {
k, v string
}
type mybasket struct {
Coupons string
Amount int
Contents []kv
}
func (t *mybasket) SetContents(c ...kv) {
t.Contents = c
return
}
func (t *mybasket) GetContents() []kv {
return t.Contents
}
func main() {
T := &mybasket{"couponlist", 100, []kv{}} // New Basket
kvs := []kv{{"foo", "bar"}, {"baz", "bat"}} // Contents
T.SetContents(kvs...) // Set Contents
fmt.Printf("%v", T.GetContents()) // Get Contents
}
Prints:
[{foo bar} {baz bat}]