English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statement

Golang Function & Method

Golang Structure

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go Function as Struct Field

In Golang, a structure or struct is a user-defined type that allows us to create a set of different types of elements in a single unit. Any real entity with a set of properties or fields can be represented as a structure. We know that functions are also user-defined types in the Go language, so you can create a function field in a Go structure. You can also create a function field in a Go structure using an anonymous function, as shown in the example2As shown.

Syntax:

type function_name func()
type strcut_name struct{
  var_name  function_name
}

Let's discuss this concept with an example:

//As a field in a Go structure
package main 
  
import "fmt"
  
// Finalsalary function type
type Finalsalary func(int, int) int
  
//Create structure
type Author struct { 
    name      string 
    language  string 
    Marticles int
    Pay       int
  
    //Function as field
    salary Finalsalary 
} 
  
func main() { 
  
    // Initialize field structure
    result := Author{ 
        name:      "Sonia", 
        language:  "Java", 
        Marticles: 120, 
        Pay:       500, 
        salary: func(Ma int, pay int) int { 
            return Ma * pay 
        , 
    } 
  
    fmt.Println("Author name: ", result.name) 
    fmt.Println("Language: ", result.language) 
    fmt.Println("Total number of articles published in May: ", result.Marticles) 
    fmt.Println("Per article payment: ", result.Pay) 
    fmt.Println("Total salary: ", result.salary(result.Marticles, result.Pay)) 
}

Output:

Author name:  Sonia
Language:  Java
Total number of articles published in May:  120
Per article payment:  500
Total salary:  60000

Using an anonymous function as a field in a Go structure2:

//Using an anonymous function as a field in a Go structure
package main 
  
import "fmt"
  
//Create structure
type Author struct { 
    name      string
    language  string
    Tarticles int
    Particles int
    Pending   func(int, int) int
} 
  
func main() { 
  
    //Initialize structure fields
    result := Author{ 
        name:      "Sonia", 
        language:  "Java", 
        Tarticles: 340, 
        Particles: 259, 
        Pending: func(Ta int, Pa int) int { 
            return Ta - Pa 
        , 
    } 
  
    fmt.Println("Author name: ", result.name) 
    fmt.Println("Language: ", result.language) 
    fmt.Println("Total number of articles: ", result.Tarticles) 
      
    fmt.Println("Total number of published articles: ", result.Particles) 
    fmt.Println("Articles to be processed: ", result.Pending(result.Tarticles, result.Particles)) 
}

Output:

Author name:  Sonia
Language:  Java
Total number of articles:  340
Total Number of Published Articles:  259
Pending Articles:  81