Go custom function types

Go allows the creation of reusable function signatures with the type keyword. In simple words, Golang also supports defining our own function types.

Go custom function types

In this example, we use the type keyword to create a function type that accepts one string parameter and returns a string.


package main

import "fmt"

type output func(string) string

func hello(name string) string {

    return fmt.Sprintf("hello %s", name)
}

func main() {
    var f output

    f = hello
    fmt.Println(f("Raj"))
}

Output:

hello Raj

Comments