GoLang

Explain Nested Structure in Go Programming Language | Go Programming Tutorial

Explain Nested Structure in Go Programming Language | Go Programming Tutorial

Explain Nested Structure in Go Programming Language | Go Programming Tutorial

In this article, you will learn about the Nested Structure in Go Programming Language.

A struct in GoLang is a user-defined type that groups the different data types into a single unit. Any real-world entity which has some properties or fields can be represented in the form of a structure. Go Language has a feature of nested structure. A structure that is defined in another structure is known as a Nested Structure. Or in other words, we can say a structure that is a field of another structure is known as a Nested Structure.
SYNTAX:
Type struct_name_1 struct {
    //Fields
}
type struct_name_2 struct {
     // variable_name
    // struct_name_1
}
Here, the struct_name_1 is the first structure which is a field of another structure name as struct_name_2

Example:

package main
  
import "fmt"
  
// Creating structure
type student_info struct {
    name   string
    branch string
    roll_no   int
}
  
// Creating nested structure
type school struct {
  
    // structure as a field
    details student_info
}
  
func main() {
  
    // Initializing the fieldsof the structure
    result := school{
      
        details: Author{"John", "CSE", 56E06},
    }
    // Display the values
    fmt.Println("\nDetails of Student")
    fmt.Println(result)
}
OUTPUT
Details of Student
{{ John CSE 56E06}}
 
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.