Nested Structures in Golang

Create new folder named entities. In entities folder, create new entities as below:

In entities folder, create new file named category.go as below:

package entities

type Category struct {
	Id   string
	Name string
}

In entities folder, create new file named manufacturer.go as below:

package entities

type Manufacturer struct {
	Id      string
	Name    string
	Address string
}

In entities folder, create new file named product.go as below:

package entities

type Product struct {
	Id           string
	Name         string
	Price        float64
	Quantity     int
	Status       bool
	Category     Category
	Manufacturer Manufacturer
}




Create new folder named src. In src folder, create new file named main.go as below:

package main

import (
	"entities"
	"fmt"
)

func main() {
	var products = []entities.Product{
		entities.Product{
			Id:       "p01",
			Name:     "tivi 1",
			Price:    5,
			Quantity: 9,
			Status:   false,
			Category: entities.Category{
				Id:   "c1",
				Name: "Category 1",
			},
			Manufacturer: entities.Manufacturer{
				Id:      "m1",
				Name:    "Manufacturer 1",
				Address: "Address 1",
			},
		},
		entities.Product{
			Id:       "p02",
			Name:     "tivi 2",
			Price:    2,
			Quantity: 8,
			Status:   true,
			Category: entities.Category{
				Id:   "c1",
				Name: "Category 1",
			},
			Manufacturer: entities.Manufacturer{
				Id:      "m1",
				Name:    "Manufacturer 1",
				Address: "Address 1",
			},
		},
		entities.Product{
			Id:       "p03",
			Name:     "laptop 3",
			Price:    11,
			Quantity: 7,
			Status:   false,
			Category: entities.Category{
				Id:   "c2",
				Name: "Category 2",
			},
			Manufacturer: entities.Manufacturer{
				Id:      "m2",
				Name:    "Manufacturer 2",
				Address: "Address 2",
			},
		},
	}

	fmt.Println("Product List")
	DisplayProductList(products)
}

func DisplayProductList(products []entities.Product) {
	for _, product := range products {
		DisplayProduct(product)
		fmt.Println("-------------------------------")
	}
}

func DisplayProduct(product entities.Product) {
	fmt.Println("id: ", product.Id)
	fmt.Println("name: ", product.Name)
	fmt.Println("status: ", product.Status)
	fmt.Println("price: ", product.Price)
	fmt.Println("quantity: ", product.Quantity)
	fmt.Println("total: ", (product.Price * float64(product.Quantity)))
	fmt.Println("Category Info")
	fmt.Println("\tid: ", product.Category.Id)
	fmt.Println("\tname: ", product.Category.Name)
	fmt.Println("Manufacturer Info")
	fmt.Println("\tid: ", product.Manufacturer.Id)
	fmt.Println("\tname: ", product.Manufacturer.Name)
	fmt.Println("\taddress: ", product.Manufacturer.Address)
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Product List
id:  p01
name:  tivi 1
status:  false
price:  5
quantity:  9
total:  45
Category Info
        id:  c1
        name:  Category 1
Manufacturer Info
        id:  m1
        name:  Manufacturer 1
        address:  Address 1
-------------------------------
id:  p02
name:  tivi 2
status:  true
price:  2
quantity:  8
total:  16
Category Info
        id:  c1
        name:  Category 1
Manufacturer Info
        id:  m1
        name:  Manufacturer 1
        address:  Address 1
-------------------------------
id:  p03
name:  laptop 3
status:  false
price:  11
quantity:  7
total:  77
Category Info
        id:  c2
        name:  Category 2
Manufacturer Info
        id:  m2
        name:  Manufacturer 2
        address:  Address 2
-------------------------------