Pointer to Structure 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() {
	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",
		},
	}

	p := &product

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




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

Product Info
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