Standard library / runtime

runtime.Caller()

Retrieves program counter, source file and line for a caller; skip zero refers to Caller's caller. Check ok before using metadata. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func Caller(skip int) (pc uintptr, file string, line int, ok bool)

Parameters & Attributes

NameTypeDescription
skipintNumber of caller frames to skip; zero identifies the caller of Caller.

Return Value

(pc uintptr, file string, line int, ok bool)

Program counter, source file, source line and a flag indicating successful lookup.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "strings")
func main() {  _, file, line, ok := runtime.Caller(0)  fmt.Println(ok && strings.HasSuffix(file, "main.go") && line > 0)}
Go Reference