Table of contents
1.
Introduction
2.
Structure in Golang
2.1.
Syntax
3.
Nested Structure
3.1.
Syntax
3.2.
Implementation
3.3.
Output
4.
Frequently Asked Questions
4.1.
What is a structure?
4.2.
What do you mean by the Nested structure?
4.3.
How do you initialise the Nested Structure in Go?
4.4.
How do you access the Nested Structure Field in Go?
5.
Conclusion
Last Updated: Mar 27, 2024

Nested Structure in Go

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope that you are doing great.

Go(also known as Golang or Go language) is a statically typedcompiled programming, an open-source programming language used for general purposes designed by Google in 2007. The main intention of creating Go was to design a programming language to address criticism of other languages by keeping their characteristics the same. 

Now Go is widely used at Google. Even many other organisations are opting for Go. It is also used in many open-source projects.

If you want to learn more about Go, then follow these articles, GoWhat is Golang?, Introduction to Go.

Today, we will be discussing the Nested Structure in Go.

This blog will help you to extend your knowledge about the Go. So, without further ado, let’s start our discussion.

Structure in Golang

structure in Golang is a user-defined type, which allows you to combine a group of elements, possibly of different types, into a single type.

Structures are used to represent different types of records and real-world entities. This concept is somehow similar to Classes in Object-Oriented Programming.

Syntax


 

If you want to learn more about structs, follow this article Structs in Go.

Nested Structure

As you saw what a structure is in Go. Now a structure can be defined as a field inside another structure. 

Simplifying the above concept. In Go, A structure which is a field of another structure is known as the Nested Structure.

Syntax

Implementation

package main

import "fmt"

type student_name struct {
    first_name string
    last_name string
}

type student struct{
    name student_name  // nested structure
    id int
}

// main function
func main() {

    // Assigning values
    value := student{ student_name{"Coding" , "Ninjas"} , 111}
    fmt.Println("First Name: " , value.name.first_name)
    fmt.Println("Second Name: ", value.name.last_name)
    fmt.Println("Id: ", value.id)

}

Output

Frequently Asked Questions

What is a structure?

A structure is a user-defined type which allows you to combine a group of elements, possibly of different types, into a single type.
 

What do you mean by the Nested structure?

In Go, A structure which is a field of another structure is known as the Nested Structure.
 

How do you initialise the Nested Structure in Go?

If you want to initialize the nested structure in Go, then you need to initialize the inner struct inside the outer structure.

For example,

type struct_inner struct{
    one int
}

type struct_outer struct {
    two int
    three struct_inner
}

You can initialize the above nested structure like below,

value := struct_outer{2 , struct_inner{1}}


How do you access the Nested Structure Field in Go?

If you want to access the nested structure, then first access the inner struct using the dot(.) operator to access the respective fields.

For example,

type struct_inner struct{
    one int
}

type struct_outer struct {
    two int
    three struct_inner
}

// initialize
value := struct_outer{2 , struct_inner{1}}

 

To access the inner structure,

value.three.one   

Conclusion

In this article, we have extensively discussed Nested Structure in Go.

We started with the basic introduction, then we discussed,

  • About the Structure in Golang
  • Nested Structure in Go
     

We hope that this blog has helped you enhance your knowledge regarding  Nested in Go and if you would like to learn more, check out our articles on  GoWhat is Golang?, Introduction to Go, Structs in Go and Dynamics in Go. Do upvote our blog to help other ninjas grow.

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Happy Reading!

Live masterclass