Struct
Basics of Struct
Struct can be used to define custom data types in Go. Often times, we can not handle the real world information using the standard data types which come with languages. While it is not impossible, it is highly inefficient. For example, in an eCommerce application, we have the ShoppingCart in which we put products for checkout.
type Product struct {
name string
itemID int
cost float32
isAvailable bool
inventoryLeft int
}
There are a lot of attributes of Product, its name, the ID used internally, the cost, number of the products in stock, etc.
name
is astring
used to store a product's name.itemID
is anint
used to store for reference.cost
is afloat32
of the item.isAvailable
is abool
which is true if the item is in stock, false otherwise.inventoryLeft
is anint
of the number of products left in stock.
Initializing
// define goBook as a Product type
var goBook Product
// assign "Webapps in Go" to the field 'name' of goBook
goBook.name = "Webapps in Go"
// assign 10025 to field 'itemID' of goBook
goBook.itemID = 10025
// access field 'name' of goBook
fmt.Printf("The product's name is %s\n", goBook.name)
There are three more ways to define a struct.
- Assign initial values by order
goBook := Product{"Webapps in Go", 10025}
- Use the format
field:value
to initialize the struct without order
goBook := Product{name:"Webapps in Go", itemID:10025, cost:100}
- Define an anonymous struct, then initialize it
p := struct{name string; age int}{"Amy",18}
Let's see a complete example.
file: code/Struct/Book/struct.go
package main
import "fmt"
// Product will denote a physical object
// which we will sell online to be rich
type Product struct {
name string
itemID int
cost float32
isAvailable bool
inventoryLeft int
}
func main() {
var goBook Product
// initialization
goBook.name, goBook.itemID, goBook.isAvailable, goBook.inventoryLeft = "Webapps in Go", 10025, true, 25
// initialize four values by format "field:value"
pythonBook := Product{itemID: 10026, name: "Learn Python", inventoryLeft: 0, isAvailable: false}
// initialize all five values in order
rubyBook := Product{"Learn Ruby", 10043, 100, true, 12}
if goBook.isAvailable {
fmt.Printf("%d copies of %s are available\n",
goBook.inventoryLeft, goBook.name)
}
if pythonBook.isAvailable {
fmt.Printf("%d copies of %s are available\n",
pythonBook.inventoryLeft, pythonBook.name)
}
if rubyBook.isAvailable {
fmt.Printf("%d copies of %s are available\n",
rubyBook.inventoryLeft, rubyBook.name)
}
}
Embedded fields in struct
In the earlier chapter, we saw how to define a struct with field names and type. Embedded fields can be thought of as subclass and superclass in Object oriented programming.
When the embedded field is a struct, all the fields in that struct will implicitly be the fields in the struct in which it has been embedded.
Let's see one example.
file: code/Struct/Human/human.go
package main
import "fmt"
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // embedded field, it means Student struct
// includes all fields that Human has.
specialty string
}
func main() {
// initialize a student
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
// access fields
fmt.Println("His name is ", mark.name)
fmt.Println("His age is ", mark.age)
fmt.Println("His weight is ", mark.weight)
fmt.Println("His specialty is ", mark.specialty)
// modify notes
mark.specialty = "AI"
fmt.Println("Mark changed his specialty")
fmt.Println("His specialty is ", mark.specialty)
// modify age
fmt.Println("Mark become old")
mark.age = 46
fmt.Println("His age is", mark.age)
// modify weight
fmt.Println("Mark is not an athlete anymore")
mark.weight += 60
fmt.Println("His weight is", mark.weight)
}
We see that we can access the age
and name
fields in Student just like we can in Human. This is how embedded fields work. We can even use Student to access Human in this embedded field!
mark.Human = Human{"Marcus", 55, 220}
mark.Human.age -= 1
All the types in Go can be used as embedded fields.
file: code/Struct/Skills/skills.go
package main
import "fmt"
type Skills []string
type Human struct {
name string
age int
weight int
}
type Student struct {
Human // struct as embedded field
Skills // string slice as embedded field
int // built-in type as embedded field
specialty string
}
func main() {
// initialize Student Jane
jane := Student{Human: Human{"Jane", 35, 100}, specialty: "Biology"}
// access fields
fmt.Println("Her name is ", jane.name)
fmt.Println("Her age is ", jane.age)
fmt.Println("Her weight is ", jane.weight)
fmt.Println("Her specialty is ", jane.specialty)
// modify value of skill field
jane.Skills = []string{"anatomy"}
fmt.Println("Her skills are ", jane.Skills)
fmt.Println("She acquired two new ones ")
jane.Skills = append(jane.Skills, "physics", "golang")
fmt.Println("Her skills now are ", jane.Skills)
// modify embedded field
jane.int = 3
fmt.Println("Her preferred number is ", jane.int)
}
In the above example, we can see that all types can be embedded fields and we can use functions to operate on them.
When we embed Human inside Employee, if Human and Employee have the phone field, then it isn't a problem. Because we access Employee's phone as Employee.Phone, but since Human is an embedded field inside Employee, we access Human's phone as Employee.Human.Phone
file: code/Struct/Employee/employee.go
package main
import "fmt"
type Human struct {
name string
age int
phone string // Human has phone field
}
type Employee struct {
Human // embedded field Human
specialty string
phone string // phone in employee
}
func main() {
Bob := Employee{Human{"Bob", 34, "777-444-XXXX"},
"Designer", "333-222"}
fmt.Println("Bob's work phone is:", Bob.phone)
// access phone field in Human
fmt.Println("Bob's personal phone is:", Bob.Human.phone)
}