-
-
Save smallnest/5e97e2d61f766e868ba74a081c9717d5 to your computer and use it in GitHub Desktop.
Methods for fetching structure fields in Go (golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
import "time" | |
import "reflect" | |
// suggested via http://stackoverflow.com/a/8363629/87207 | |
func trace(s string) (string, time.Time) { | |
log.Println("START:", s) | |
return s, time.Now() | |
} | |
// suggested via http://stackoverflow.com/a/8363629/87207 | |
func un(s string, startTime time.Time) { | |
endTime := time.Now() | |
log.Println(" END:", s, "ElapsedTime in seconds:", endTime.Sub(startTime)) | |
} | |
type S struct { | |
A int | |
B int | |
C int | |
D int | |
E int | |
F int | |
G int | |
H int | |
X int | |
Y int | |
} | |
type Getter func(*S, string) int | |
// get a field by static offset | |
func getField(s *S, _ string) int { | |
return s.X | |
} | |
// dynamically lookup a field | |
// 40x slower than getField() | |
func reflectField(s *S, field string) int { | |
r := reflect.ValueOf(s) | |
f := reflect.Indirect(r).FieldByName(field) | |
return int(f.Int()) | |
} | |
// create a function that knows what slot to fetch given a name. | |
// 10x slower than getField() | |
func makeFieldGetter(v reflect.Value, field string) Getter { | |
t := v.Type() | |
for i := 0; i < v.NumField(); i++ { | |
if t.Field(i).Name == field { | |
return func(s *S, _ string) int { | |
av := reflect.ValueOf(s).Elem() | |
i, _ := av.Field(i).Interface().(int) | |
return i | |
} | |
} | |
} | |
return nil | |
} | |
func testMethod(name string, s *S, f Getter) { | |
defer un(trace(name)) | |
for i := 0; i < 10 * 1000 * 1000; i++ { | |
x := f(s, "X") | |
x = x | |
} | |
} | |
func main() { | |
s := S{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | |
testMethod("direct", &s, getField) | |
testMethod("getter", &s, makeFieldGetter(reflect.ValueOf(&s).Elem(), "X")) | |
testMethod("reflect", &s, reflectField) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment