Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

User defined function types in Go (Golang)

Posted on March 7, 2023March 7, 2023 by admin

Table of Contents

  • Overview
  • Code
    • Example 1
    • Example 2

Overview

In GO, the function is also a type. Two functions will be of the same type if

  • They have the same number of arguments with each argument is of the same type
  • They have the same number of return values and each return value is of the same type

Function as user-defined type can be declared using the type keyword like below. area is the name of the function of type func(int, int) int

type area func(int, int) int

Code

Example 1

In this example, we create a user-defined function type area. Then we create a variable of type area in the main function.

package main
import "fmt"
type area func(int, int) int
func main() {
var areaF area = func(a, b int) int {
return a * b
}
print(2, 3, areaF)
}
func print(x, y int, a area) {
fmt.Printf("Area is: %d\n", a(x, y))
}

Output:

6

Example 2

In this example also we create a user-defined function type area. Then we create a function getAreaFunc() which returns the function of type area

package main
import "fmt"
type area func(int, int) int
func main() {
areaF := getAreaFunc()
print(2, 3, areaF)
}
func print(x, y int, a area) {
fmt.Printf("Area is: %d\n", a(x, y))
}
func getAreaFunc() area {
return func(x, y int) int {
return x * y
}
}

Output:

6
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP