funcname

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 2

README

funcname

Go Reference Go Report Card

funcname is a Go package that 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.

Installation

To install the funcname package, use the following command:

go get github.com/goaux/funcname

Usage

Here are some examples of how to use the funcname package:

package main

import (
    "fmt"
    "github.com/goaux/funcname"
)

func main() {
    // Get the name of the current function
    fmt.Println(funcname.This())

    // Get the name of a function from an interface
    fmt.Println(funcname.Of(fmt.Println))

    // Split a function name into package and function components
    pkg, fn := funcname.Split("github.com/user/pkg.Func")
    fmt.Printf("Package: %s, Function: %s\n", pkg, fn)
}

API

The funcname package provides the following main functions:

  • Of(v interface{}) string: Returns the full name of the function or method represented by v.
  • SplitOf(v interface{}) (pkgname, name string): Returns the package name and function name of the function or method represented by v.
  • This() string: Returns the name of the calling function.
  • SplitThis() (pkgname, name string): Returns the package name and function name of the calling function.
  • Caller(skip int) string: Returns the name of the calling function, skipping the specified number of stack frames.
  • SplitCaller(skip int) (pkgname, name string): Returns the package name and function name of the calling function, skipping the specified number of stack frames.
  • ForPC(pc uintptr) string: Returns the name of the function corresponding to the given program counter.
  • SplitForPC(pc uintptr) (pkgname, name string): Returns the package name and function name corresponding to the given program counter.
  • Split(s string) (pkgname, name string): Separates a fully qualified function name into package name and function name.

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

Examples

Constants

View Source
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

func Caller(skip int) string

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

func ForPC(pc uintptr) string

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

func Split(s string) (pkgname, name string)

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

func SplitCaller(skip int) (pkgname, name string)

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

func SplitForPC(pc uintptr) (pkgname, name string)

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).

func SplitThis

func SplitThis() (pkgname, name string)

SplitThis returns the package name and function name of the calling function. It is equivalent to Split(Caller(1)).

func This

func This() string

This returns the name of the calling function. It is equivalent to Caller(1).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL