Thanks for the all the responses but I was after a more dynamic solution
where you don't need to 'register' the types or select them using a switch
but that it just works.
For example, to support new types (like the PHP code) I was hoping you
could just add the structs and no more changes to the code base, it just
magically supports any new Json types.
I was going down this route:
package main
import (
"fmt"
"go/importer"
"os"
"reflect"
"unsafe"
)
type dummyInterface struct {
_ uintptr
_ uint64
_ [3]uintptr
StrPtr *string
}
var rules map[string]reflect.Type
func init() {
rules = make(map[string]reflect.Type)
pkg, err := importer.Default().Import("test/rule")
if err != nil {
fmt.Printf("error: %s\n", err.Error())
os.Exit(1)
}
for _, typeName := range pkg.Scope().Names() {
name := "rule." + typeName
var rule interface{} = &dummyInterface{
StrPtr: &name,
}
typ := reflect.TypeOf(*(*interface{})(unsafe.Pointer(&rule)))
rules[name] = typ
}
}
func main() {
for _, r := range rules {
c := reflect.New(r)
fmt.Printf("%#v\n", c.Elem())
}
}
This code currently doesn't work but you can see where I was going. I only
had a small measure of success (in some iterations I did get it working)
and the more I try to make this work I realise it's going too far.
I think a simple switch is much more in keeping with Go.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.