NameOf returns the name of the function value.
(v reflect.Value)
| 76 | |
| 77 | // NameOf returns the name of the function value. |
| 78 | func NameOf(v reflect.Value) string { |
| 79 | fnc := runtime.FuncForPC(v.Pointer()) |
| 80 | if fnc == nil { |
| 81 | return "<unknown>" |
| 82 | } |
| 83 | fullName := fnc.Name() // e.g., "long/path/name/mypkg.(*MyType).(long/path/name/mypkg.myMethod)-fm" |
| 84 | |
| 85 | // Method closures have a "-fm" suffix. |
| 86 | fullName = strings.TrimSuffix(fullName, "-fm") |
| 87 | |
| 88 | var name string |
| 89 | for len(fullName) > 0 { |
| 90 | inParen := strings.HasSuffix(fullName, ")") |
| 91 | fullName = strings.TrimSuffix(fullName, ")") |
| 92 | |
| 93 | s := lastIdentRx.FindString(fullName) |
| 94 | if s == "" { |
| 95 | break |
| 96 | } |
| 97 | name = s + "." + name |
| 98 | fullName = strings.TrimSuffix(fullName, s) |
| 99 | |
| 100 | if i := strings.LastIndexByte(fullName, '('); inParen && i >= 0 { |
| 101 | fullName = fullName[:i] |
| 102 | } |
| 103 | fullName = strings.TrimSuffix(fullName, ".") |
| 104 | } |
| 105 | return strings.TrimSuffix(name, ".") |
| 106 | } |