| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"runtime" |
| 11 |
) |
| 12 |
|
| 13 |
func main() { |
| 14 |
println(caller().frame.Function) |
| 15 |
|
| 16 |
// Used to erroneously print "main.call.name" instead of |
| 17 |
// "main.main". |
| 18 |
println(caller().name()) |
| 19 |
} |
| 20 |
|
| 21 |
func caller() call { |
| 22 |
var pcs [3]uintptr |
| 23 |
n := runtime.Callers(1, pcs[:]) |
| 24 |
frames := runtime.CallersFrames(pcs[:n]) |
| 25 |
frame, _ := frames.Next() |
| 26 |
frame, _ = frames.Next() |
| 27 |
|
| 28 |
return call{frame: frame} |
| 29 |
} |
| 30 |
|
| 31 |
type call struct { |
| 32 |
frame runtime.Frame |
| 33 |
} |
| 34 |
|
| 35 |
func (c call) name() string { |
| 36 |
return c.frame.Function |
| 37 |
} |