Search code examples
reflectioncastinggo

Go: check whether type of value is function


How I can check that some variable in golang has type func, like this:

func A() {}

func main() {
    variable := A
    isFunc := IsFunc(variable) // true or false
}

Solution

  • func IsFunc(v any) bool {
       return reflect.TypeOf(v).Kind() == reflect.Func
    }