Task

Explain or demonstrate the levels of visibility of function names and labels within the language.



See also

 Variables for levels of scope relating to visibility of program variables
 Scope modifiers for general scope modification facilities


package main
 
import (
    "fmt"
    "runtime"
 
    "ex"
)
 
func main() {
    // func nested() { ... not allowed here
 
    // this is okay, variable f declared and assigned a function literal.
    f := func() {
        // this mess prints the name of the function to show that it's an
        // anonymous function defined in package main
        pc, _, _, _ := runtime.Caller(0)
        fmt.Println(runtime.FuncForPC(pc).Name(), "here!")
    }
 
    ex.X(f) // function value passed to exported function
 
    // ex.x() non-exported function not visible here
}