inspiration:
http://godoc.org/github.com/kylelemons/godebug/pretty
http://godoc.org/github.com/davecgh/go-spew/spew
etc
On Tue, Apr 2, 2013 at 11:18 PM, dlin wrote:
http://play.golang.org/p/OKde_oM5me
package main
import "reflect"
import "fmt"
type THeader struct {
H1 int8
H2 byte
}
type TBody struct {
B1 [5]byte
B2 [10]byte
}
func main() {
th := THeader{1, 2}
fmt.Println(Show(th))
tb := TBody{}
fmt.Println(Show(tb))
}
func Show(t interface{}) (s string) {
//func Show(t THeader) (s string) { // if change to this line is OK, but
I require to write for every structure.
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
f := reflect.ValueOf(&t).Elem().Field(i)
if !p.Anonymous {
s += fmt.Sprintln(p.Name, f.Interface())
}
}
return
}
panic: reflect: call of reflect.Value.Field on interface Value
goroutine 1 [running]:
reflect.flag.mustBe(0x146, 0x19, 0x1600000000, 0x44f1e8)
go/src/pkg/reflect/value.go:241 +0xa2
reflect.Value.Field(0x450998, 0xf8400240c0, 0x146, 0x0, 0xf8400240c0, ...)
go/src/pkg/reflect/value.go:637 +0x32
main.Show(0x4754a8, 0x746e797300000201, 0x0, 0x746e797300000000, 0xf8400235d0, ...)
/tmpfs/gosandbox-3a2d77a5_b0f13ffe_6a64df0c_5238e1a8_846b332c/prog.go:28 +0x1ea
main.main()
/tmpfs/gosandbox-3a2d77a5_b0f13ffe_6a64df0c_5238e1a8_846b332c/prog.go:16 +0x54
--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--http://play.golang.org/p/OKde_oM5me
package main
import "reflect"
import "fmt"
type THeader struct {
H1 int8
H2 byte
}
type TBody struct {
B1 [5]byte
B2 [10]byte
}
func main() {
th := THeader{1, 2}
fmt.Println(Show(th))
tb := TBody{}
fmt.Println(Show(tb))
}
func Show(t interface{}) (s string) {
//func Show(t THeader) (s string) { // if change to this line is OK, but
I require to write for every structure.
typ := reflect.TypeOf(t)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
f := reflect.ValueOf(&t).Elem().Field(i)
if !p.Anonymous {
s += fmt.Sprintln(p.Name, f.Interface())
}
}
return
}
panic: reflect: call of reflect.Value.Field on interface Value
goroutine 1 [running]:
reflect.flag.mustBe(0x146, 0x19, 0x1600000000, 0x44f1e8)
go/src/pkg/reflect/value.go:241 +0xa2
reflect.Value.Field(0x450998, 0xf8400240c0, 0x146, 0x0, 0xf8400240c0, ...)
go/src/pkg/reflect/value.go:637 +0x32
main.Show(0x4754a8, 0x746e797300000201, 0x0, 0x746e797300000000, 0xf8400235d0, ...)
/tmpfs/gosandbox-3a2d77a5_b0f13ffe_6a64df0c_5238e1a8_846b332c/prog.go:28 +0x1ea
main.main()
/tmpfs/gosandbox-3a2d77a5_b0f13ffe_6a64df0c_5238e1a8_846b332c/prog.go:16 +0x54
--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.