From 34e2680236725e6a4da1ca253c01afc173f66d0d Mon Sep 17 00:00:00 2001 From: Craig Hesling Date: Tue, 24 Apr 2018 23:34:21 -0400 Subject: [PATCH 1/3] Minor syntax errors in test - t.Error to t.Errorf --- forceexport_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forceexport_test.go b/forceexport_test.go index 49b0f0e..32edf3c 100644 --- a/forceexport_test.go +++ b/forceexport_test.go @@ -41,7 +41,7 @@ func TestGetSelf(t *testing.T) { var getFunc func(interface{}, string) error err := GetFunc(&getFunc, "github.com/alangpierce/go-forceexport.GetFunc") if err != nil { - t.Error("Error: %s", err) + t.Errorf("Error: %s", err) } // The two functions should share the same code pointer, so they should // have the same string representation. @@ -51,7 +51,7 @@ func TestGetSelf(t *testing.T) { // Call it again on itself! err = getFunc(&getFunc, "github.com/alangpierce/go-forceexport.GetFunc") if err != nil { - t.Error("Error: %s", err) + t.Errorf("Error: %s", err) } if fmt.Sprint(getFunc) != fmt.Sprint(GetFunc) { t.Errorf("Expected ") From a717f53cda07c750b3c58a305128deb8694872b6 Mon Sep 17 00:00:00 2001 From: Craig Hesling Date: Wed, 25 Apr 2018 01:06:22 -0400 Subject: [PATCH 2/3] Evade go vet's error about function in fmt.Sprint --- forceexport_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/forceexport_test.go b/forceexport_test.go index 32edf3c..8dd518f 100644 --- a/forceexport_test.go +++ b/forceexport_test.go @@ -2,9 +2,16 @@ package forceexport import ( "fmt" + "reflect" + "runtime" "testing" ) +// funcName resolves the name of a given function +func funcName(f interface{}) string { + return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() +} + func TestTimeNow(t *testing.T) { var timeNowFunc func() (int64, int32) GetFunc(&timeNowFunc, "time.now") @@ -45,7 +52,7 @@ func TestGetSelf(t *testing.T) { } // The two functions should share the same code pointer, so they should // have the same string representation. - if fmt.Sprint(getFunc) != fmt.Sprint(GetFunc) { + if fmt.Sprint(funcName(getFunc)) != fmt.Sprint(funcName(GetFunc)) { t.Errorf("Expected ") } // Call it again on itself! @@ -53,7 +60,7 @@ func TestGetSelf(t *testing.T) { if err != nil { t.Errorf("Error: %s", err) } - if fmt.Sprint(getFunc) != fmt.Sprint(GetFunc) { + if fmt.Sprint(funcName(getFunc)) != fmt.Sprint(funcName(GetFunc)) { t.Errorf("Expected ") } } From 5e795da1275d6bb103537d21d91c6310f24ffe9f Mon Sep 17 00:00:00 2001 From: Craig Hesling Date: Wed, 25 Apr 2018 01:08:58 -0400 Subject: [PATCH 3/3] Added rough benchmark --- forceexport_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/forceexport_test.go b/forceexport_test.go index 8dd518f..89edfb2 100644 --- a/forceexport_test.go +++ b/forceexport_test.go @@ -75,3 +75,12 @@ func TestInvalidFunc(t *testing.T) { t.Error("Expected a nil function.") } } + +// BenchmarkGetMainInit check how long it takes to find the symbol main.init, +// which is typically the last func symbol(by experiment). +func BenchmarkGetMainInit(b *testing.B) { + for i := 0; i < b.N; i++ { + var main_init func() + GetFunc(&main_init, "main.init") + } +}