Entities
Create new folder named entities. In entities folder, create new entities as below:
Category Entity
In entities folder, create new file named category.go as below:
package entities
type Category struct {
Id string
Name string
}
Manufacturer Entity
In entities folder, create new file named manufacturer.go as below:
package entities
type Manufacturer struct {
Id string
Name string
Address string
}
Product Entity
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
}
Application
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)
}
Output
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