Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

How to intialize a struct that has another nested struct in Go (Golang)

Posted on March 4, 2023March 4, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

A struct can have another struct nested in it. Let’s see an example of a nested struct. In below example employee struct has the address struct nested it in.

type address struct {
city string
country string
}
type employee struct {
name string
age int
salary int
address address
}

To initialize such kind of struct we need to initialize the nested struct first. That is first we will initialize the address struct. For eg as below

address := address{city: "London", country: "UK"}

Then we can initialize the employee struct as below

emp := employee{name: "Sam", age: 31, salary: 2000, address: address}

Another way is to directly initialize the address struct during the initialization of the employee struct. Like as below

emp := employee{name: "Sam", age: 31, salary: 2000, address: address{city: "London", country: "UK"}}

Program

package main
import "fmt"
type employee struct {
name string
age int
salary int
address address
}
type address struct {
city string
country string
}
func main() {
address := address{city: "London", country: "UK"}
emp := employee{name: "Sam", age: 31, salary: 2000, address: address}
fmt.Printf("City: %s\n", emp.address.city)
fmt.Printf("Country: %s\n", emp.address.country)
}

Output

City: London
Country: UK
  • go
  • 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