Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Don’t Use
reflect
Go 1.7 Release Party
Aug 8, 2016
Daisuke Maki @lestrrat
Don’t Use
reflect
Go 1.7 Release Party
Aug 8, 2016
Daisuke Maki @lestrrat
Don’t Use
reflect
Go 1.7 Release Party
Aug 8, 2016
Daisuke Maki @lestrrat
New Go Book!
無念!
Reflect
Dynamically
Change Behavior
All the cool kids are doing it
… in Go?
Performance
From The Book
Line Noise
@P=split//,".URRUUc8R";@d=split//,"nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q...
Line Noise
in Go?
reflect.TypeOf((*Maybe)(nil)).Elem()
reflect.TypeOf((*Maybe)(nil)).Elem()
WTF
reflect.TypeOf((*Maybe)(nil)).Elem()
reflect.TypeOf((*Maybe)(nil)).Elem()
pointer to nil “Maybe”
reflect.TypeOf((*Maybe)(nil)).Elem()
reflect.TypeOf((*Maybe)(nil)).Elem()
Dereference the pointer reflect.Type
It’s an
Interface Type!
stringer = reflect.

TypeOf((*Stringer)(nil)).Elem()
if rv.Implements(stringer) {
…

}
Check If A Value Implements An Inte...
• reflect.Type/Value usually requires a value to create
• But if you don’t want to create a variable just to create
an int...
Why Use reflect?
意訳「そこにreflectがあるから」
Sometimes, you
just have to
Otherwise, DON’T
New in Go1.7
禁句
「この機能、誰が嬉しいんだ?」
rv := reflect.ValueOf(…)
fv := rv.Field(1<<63-1) // 1.7 PANICs
if fv == reflect.Value{} {
// <1.6 silently returns zero va...
rv := reflect.ValueOf(…)
fv := rv.Field(1<<63-1) // 1.7 PANICs
if fv == reflect.Value{} {
// <1.6 silently returns zero va...
rv := reflect.ValueOf(…)
fv := rv.Field(1<<63-1) // 1.7 PANICs
if fv == reflect.Value{} {
// <1.6 silently returns zero va...
type Foo struct {
foo int // not exported
}
Bug? Fix
_, ok := reflect.
TypeOf(Foo{}).
MethodByName(“foo”)
fmt.Printf(“ok = %tn”)
Bug? Fix
_, ok := reflect.
TypeOf(Foo{}).
MethodByName(“foo”)
fmt.Printf(“ok = %tn”)
Bug? Fix
// go1.6 -> true
_, ok := reflect.
TypeOf(Foo{}).
MethodByName(“foo”)
fmt.Printf(“ok = %tn”)
Bug? Fix
// go1.7 -> false
// struct { Foo int; Bar string }
typ := reflect.StructOf([]reflect.StructField{
reflect.StructField{ Name: “Foo”, Type: r...
rv := reflect.New(typ)
// v.Foo = 100
fv := rv.Elem().
FieldByName(“Foo”).
Set(reflect.ValueOf(100))
// &struct { Foo int;...
field := fv.FieldByName(“Foo”)
field.Tag.Get(“json”) // string
field.Tag.Lookup(“json”) // string, bool
Can Tell Differenc...
Happy hacking
with Go1.7
…and Don’t Use
reflect! :D
Don't Use Reflect - Go 1.7 release party 2016
Don't Use Reflect - Go 1.7 release party 2016
Don't Use Reflect - Go 1.7 release party 2016
Upcoming SlideShare
Loading in …5
×

Don't Use Reflect - Go 1.7 release party 2016

1,719 views

Published on

short talk on dangers of reflect and some new stuff from go1.7

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Don't Use Reflect - Go 1.7 release party 2016

  1. 1. Don’t Use reflect Go 1.7 Release Party Aug 8, 2016 Daisuke Maki @lestrrat
  2. 2. Don’t Use reflect Go 1.7 Release Party Aug 8, 2016 Daisuke Maki @lestrrat
  3. 3. Don’t Use reflect Go 1.7 Release Party Aug 8, 2016 Daisuke Maki @lestrrat
  4. 4. New Go Book!
  5. 5. 無念!
  6. 6. Reflect
  7. 7. Dynamically Change Behavior
  8. 8. All the cool kids are doing it
  9. 9. … in Go?
  10. 10. Performance
  11. 11. From The Book
  12. 12. Line Noise
  13. 13. @P=split//,".URRUUc8R";@d=split//,"nrekcah xinU / lreP rehtona tsuJ";sub p{ @p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord ($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&& close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/S/;print
  14. 14. Line Noise in Go?
  15. 15. reflect.TypeOf((*Maybe)(nil)).Elem()
  16. 16. reflect.TypeOf((*Maybe)(nil)).Elem() WTF
  17. 17. reflect.TypeOf((*Maybe)(nil)).Elem()
  18. 18. reflect.TypeOf((*Maybe)(nil)).Elem() pointer to nil “Maybe”
  19. 19. reflect.TypeOf((*Maybe)(nil)).Elem()
  20. 20. reflect.TypeOf((*Maybe)(nil)).Elem() Dereference the pointer reflect.Type
  21. 21. It’s an Interface Type!
  22. 22. stringer = reflect.
 TypeOf((*Stringer)(nil)).Elem() if rv.Implements(stringer) { …
 } Check If A Value Implements An Interface
  23. 23. • reflect.Type/Value usually requires a value to create • But if you don’t want to create a variable just to create an interface reflect.Type… • Create a nil pointer, then dereference it! reflect.TypeOf((*Maybe)(nil)).Elem()
  24. 24. Why Use reflect?
  25. 25. 意訳「そこにreflectがあるから」
  26. 26. Sometimes, you just have to
  27. 27. Otherwise, DON’T
  28. 28. New in Go1.7
  29. 29. 禁句
  30. 30. 「この機能、誰が嬉しいんだ?」
  31. 31. rv := reflect.ValueOf(…) fv := rv.Field(1<<63-1) // 1.7 PANICs if fv == reflect.Value{} { // <1.6 silently returns zero value } Bug Fix
  32. 32. rv := reflect.ValueOf(…) fv := rv.Field(1<<63-1) // 1.7 PANICs if fv == reflect.Value{} { // <1.6 silently returns zero value } Bug Fix
  33. 33. rv := reflect.ValueOf(…) fv := rv.Field(1<<63-1) // 1.7 PANICs if fv == reflect.Value{} { // <1.6 silently returns zero value } Bug Fix
  34. 34. type Foo struct { foo int // not exported } Bug? Fix
  35. 35. _, ok := reflect. TypeOf(Foo{}). MethodByName(“foo”) fmt.Printf(“ok = %tn”) Bug? Fix
  36. 36. _, ok := reflect. TypeOf(Foo{}). MethodByName(“foo”) fmt.Printf(“ok = %tn”) Bug? Fix // go1.6 -> true
  37. 37. _, ok := reflect. TypeOf(Foo{}). MethodByName(“foo”) fmt.Printf(“ok = %tn”) Bug? Fix // go1.7 -> false
  38. 38. // struct { Foo int; Bar string } typ := reflect.StructOf([]reflect.StructField{ reflect.StructField{ Name: “Foo”, Type: reflect.Int}, reflect.StructField{ Name: “Bar”, Type: reflect.String}, }) Dynamically Declare Anonymous Structs
  39. 39. rv := reflect.New(typ) // v.Foo = 100 fv := rv.Elem(). FieldByName(“Foo”). Set(reflect.ValueOf(100)) // &struct { Foo int; Bar string }{Foo:100, Bar:""} fmt.Printf(“%#vn”, rv.Interface()) Dynamically Declare Anonymous Structs
  40. 40. field := fv.FieldByName(“Foo”) field.Tag.Get(“json”) // string field.Tag.Lookup(“json”) // string, bool Can Tell Difference Between empty tag and non-existing tag
  41. 41. Happy hacking with Go1.7
  42. 42. …and Don’t Use reflect! :D

×