Documentation
¶
Overview ¶
Package funcname provides utility functions for retrieving and manipulating function names. It offers various methods to obtain function names from different contexts, such as runtime program counters, interface values, and the current call stack.
Index ¶
- Constants
- func Caller(skip int) string
- func ForPC(pc uintptr) string
- func Of(v interface{}) string
- func Split(s string) (pkgname, name string)
- func SplitCaller(skip int) (pkgname, name string)
- func SplitForPC(pc uintptr) (pkgname, name string)
- func SplitOf(v interface{}) (pkgname, name string)
- func SplitThis() (pkgname, name string)
- func This() string
Examples ¶
Constants ¶
const ( // Unknown is the full name returned when a function name cannot be determined. // It is a combination of UnknownPackage and UnknownFunction. Unknown = UnknownPackage + "." + UnknownFunction // UnknownPackage is the package name returned when a package cannot be determined. UnknownPackage = "(unknown)" // UnknownFunction is the function name returned when a function cannot be determined. UnknownFunction = "(Unknown)" )
Variables ¶
This section is empty.
Functions ¶
func Caller ¶
Caller returns the name of the calling function, skipping the specified number of stack frames. It wraps ForPC(pc) where pc is taken from runtime.Caller(skip + 1). If runtime.Caller fails to retrieve the program counter, it returns Unknown.
func ForPC ¶
ForPC returns the name of the function corresponding to the given program counter. It wraps runtime.FuncForPC(pc).Name(). If the function cannot be found, it returns Unknown.
func Of ¶
func Of(v interface{}) string
Of returns the full name of the function or method represented by v. It wraps ForPC(pc) where pc is taken from reflect.ValueOf(v).Pointer(). The parameter v must be a function or a method. If v is not a function or method, it returns Unknown.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/goaux/funcname"
)
func main() {
fmt.Println(funcname.Of(strings.ToUpper))
}
Output: strings.ToUpper
func Split ¶
Split separates a fully qualified function name into package name and function name. It handles three main cases: 1. Full path with package and function: "github.com/user/pkg.Func" -> ("github.com/user/pkg", "Func") 2. Package and function without full path: "pkg.Func" -> ("pkg", "Func") 3. Only package name: "pkg" -> ("pkg", "")
The function works by finding the last '/' character (if any) to separate the path, then finding the first '.' character after that to separate the package and function names. If there's no '.' character, it assumes the entire string is the package name.
Example ¶
package main
import (
"fmt"
"github.com/goaux/funcname"
)
func main() {
pkg, fn := funcname.Split("github.com/user/pkg.Func")
fmt.Printf("Package: %s, Function: %s\n", pkg, fn)
}
Output: Package: github.com/user/pkg, Function: Func
func SplitCaller ¶
SplitCaller returns the package name and function name of the calling function, skipping the specified number of stack frames. It is equivalent to Split(Caller(skip + 1)).
func SplitForPC ¶
SplitForPC returns the package name and function name corresponding to the given program counter. It is equivalent to Split(ForPC(pc)).
func SplitOf ¶
func SplitOf(v interface{}) (pkgname, name string)
SplitOf returns the package name and function name of the function or method represented by v. It wraps Split(Of(v)). If v is not a function or method, it returns (UnknownPackage, UnknownFunction).
Types ¶
This section is empty.