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.
typeProductstruct { 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 a string used to store a product's name.
itemID is an int used to store for reference.
cost is a float32 of the item.
isAvailable is a bool which is true if the item is in stock, false otherwise.
inventoryLeft is an int of the number of products left in stock.
Initializing
// define goBook as a Product typevar goBook Product// assign "Webapps in Go" to the field 'name' of goBookgoBook.name ="Webapps in Go"// assign 10025 to field 'itemID' of goBookgoBook.itemID =10025// access field 'name' of goBookfmt.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
packagemainimport"fmt"// Product will denote a physical object// which we will sell online to be richtypeProductstruct { name string itemID int cost float32 isAvailable bool inventoryLeft int}funcmain() {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
packagemainimport"fmt"typeHumanstruct { name string age int weight int}typeStudentstruct {Human// embedded field, it means Student struct// includes all fields that Human has. specialty string}funcmain() {// 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!
All the types in Go can be used as embedded fields.
file: code/Struct/Skills/skills.go
packagemainimport"fmt"typeSkills []stringtypeHumanstruct { name string age int weight int}typeStudentstruct {Human// struct as embedded fieldSkills// string slice as embedded fieldint// built-in type as embedded field specialty string}funcmain() {// 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
packagemainimport"fmt"typeHumanstruct { name string age int phone string// Human has phone field}typeEmployeestruct {Human// embedded field Human specialty string phone string// phone in employee}funcmain() { 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)}