Declare Structure in Golang

Create new folder named entities. 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
}

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

package main

import (
	"entities"
	"fmt"
)

func main() {
	fmt.Println("Demo 1")
	Demo1()

	fmt.Println("Demo 2")
	Demo2()
}

func Demo1() {
	var product entities.Product
	product.Id = "p01"
	product.Name = "name 1"
	product.Price = 4.5
	product.Quantity = 20
	product.Status = true
	fmt.Println("Product Info")
	fmt.Println("id: ", product.Id)
	fmt.Println("name: ", product.Name)
	fmt.Println("price: ", product.Price)
	fmt.Println("quantity: ", product.Quantity)
	fmt.Println("status: ", product.Status)
	fmt.Println("total: ", (product.Price * float64 (product.Quantity)))
}

func Demo2() {
	product := entities.Product {
		Id:       "p02",
		Name:     "name 2",
		Price:    5,
		Quantity: 9,
		Status:   false,
	}
	fmt.Println("Product Info")
	fmt.Println("id: ", product.Id)
	fmt.Println("name: ", product.Name)
	fmt.Println("price: ", product.Price)
	fmt.Println("quantity: ", product.Quantity)
	fmt.Println("status: ", product.Status)
	fmt.Println("total: ", (product.Price * float64 (product.Quantity)))
}




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

Demo 1
Product Info
id:  p01
name:  name 1
price:  4.5
quantity:  20
status:  true
total:  90

Demo 2
Product Info
id:  p02
name:  name 2
price:  5
quantity:  9
status:  false
total:  45