SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
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*=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
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 Interface
• 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()
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 value
}
Bug Fix
rv := reflect.ValueOf(…)
fv := rv.Field(1<<63-1) // 1.7 PANICs
if fv == reflect.Value{} {
// <1.6 silently returns zero value
}
Bug Fix
rv := reflect.ValueOf(…)
fv := rv.Field(1<<63-1) // 1.7 PANICs
if fv == reflect.Value{} {
// <1.6 silently returns zero value
}
Bug Fix
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: reflect.Int},
reflect.StructField{ Name: “Bar”, Type: reflect.String},
})
Dynamically Declare
Anonymous Structs
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
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
Happy hacking
with Go1.7
…and Don’t Use
reflect! :D

Mais conteúdo relacionado

Mais de lestrrat

YAPC::Asia Tokyo 2012 Closing
YAPC::Asia Tokyo 2012 ClosingYAPC::Asia Tokyo 2012 Closing
YAPC::Asia Tokyo 2012 Closing
lestrrat
 
YAPC::Asia Tokyo 2011 Closing
YAPC::Asia Tokyo 2011 ClosingYAPC::Asia Tokyo 2011 Closing
YAPC::Asia Tokyo 2011 Closing
lestrrat
 

Mais de lestrrat (20)

Oxygen Not Includedをやるべき4つの理由
Oxygen Not Includedをやるべき4つの理由Oxygen Not Includedをやるべき4つの理由
Oxygen Not Includedをやるべき4つの理由
 
Rejectcon 2018
Rejectcon 2018Rejectcon 2018
Rejectcon 2018
 
Builderscon tokyo 2018 speaker dinner
Builderscon tokyo 2018 speaker dinnerBuilderscon tokyo 2018 speaker dinner
Builderscon tokyo 2018 speaker dinner
 
GoらしいAPIを求める旅路 (Go Conference 2018 Spring)
GoらしいAPIを求める旅路 (Go Conference 2018 Spring)GoらしいAPIを求める旅路 (Go Conference 2018 Spring)
GoらしいAPIを求める旅路 (Go Conference 2018 Spring)
 
Google container builderと友だちになるまで
Google container builderと友だちになるまでGoogle container builderと友だちになるまで
Google container builderと友だちになるまで
 
筋肉によるGoコードジェネレーション
筋肉によるGoコードジェネレーション筋肉によるGoコードジェネレーション
筋肉によるGoコードジェネレーション
 
iosdc 2017
iosdc 2017iosdc 2017
iosdc 2017
 
シュラスコの食べ方 超入門
シュラスコの食べ方 超入門シュラスコの食べ方 超入門
シュラスコの食べ方 超入門
 
OSSの敵になるのもいいじゃない
OSSの敵になるのもいいじゃないOSSの敵になるのもいいじゃない
OSSの敵になるのもいいじゃない
 
Coding in the context era
Coding in the context eraCoding in the context era
Coding in the context era
 
Kubernetes in 30 minutes (2017/03/10)
Kubernetes in 30 minutes (2017/03/10)Kubernetes in 30 minutes (2017/03/10)
Kubernetes in 30 minutes (2017/03/10)
 
Opening: builderscon tokyo 2016
Opening: builderscon tokyo 2016Opening: builderscon tokyo 2016
Opening: builderscon tokyo 2016
 
Kubernetes in 20 minutes - HDE Monthly Technical Session 24
Kubernetes in 20 minutes - HDE Monthly Technical Session 24Kubernetes in 20 minutes - HDE Monthly Technical Session 24
Kubernetes in 20 minutes - HDE Monthly Technical Session 24
 
小規模でもGKE - DevFest Tokyo 2016
小規模でもGKE - DevFest Tokyo 2016小規模でもGKE - DevFest Tokyo 2016
小規模でもGKE - DevFest Tokyo 2016
 
いまさら聞けないselectあれこれ
いまさら聞けないselectあれこれいまさら聞けないselectあれこれ
いまさら聞けないselectあれこれ
 
How To Think In Go
How To Think In GoHow To Think In Go
How To Think In Go
 
On internationalcommunityrelations
On internationalcommunityrelationsOn internationalcommunityrelations
On internationalcommunityrelations
 
Yapceu 2015 yapc asia tokyo behind the scenes (w/o notes)
Yapceu 2015 yapc asia tokyo behind the scenes (w/o notes)Yapceu 2015 yapc asia tokyo behind the scenes (w/o notes)
Yapceu 2015 yapc asia tokyo behind the scenes (w/o notes)
 
YAPC::Asia Tokyo 2012 Closing
YAPC::Asia Tokyo 2012 ClosingYAPC::Asia Tokyo 2012 Closing
YAPC::Asia Tokyo 2012 Closing
 
YAPC::Asia Tokyo 2011 Closing
YAPC::Asia Tokyo 2011 ClosingYAPC::Asia Tokyo 2011 Closing
YAPC::Asia Tokyo 2011 Closing
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Don't Use Reflect - Go 1.7 release party 2016