Types - Practical Go Lessons-13
Types - Practical Go Lessons-13
com/chap-13-types
• Value set
• Predeclared types
• Type struct
• Composite types
• Fields
• Embedded fields
3 What is a type?
“A type determines a set of values together with operations and methods specific to those values.”1. Let’s decompose this definition :
• A set of values.…
◦ A variable of type uint32 can contain all values from 0 to 4.294.967.295. Those 4+ million values are the set of values allowed
by this type.
◦ the string “Go is Great” is not an uint32 , this value does not belong to the set of values allowed by this type
1 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
• ... with operation and methods specific to those values. Operations and methods are capabilities that are shipped with values of a
type.
◦ Types have a set of operations and methods that we can apply to values of those types.
◦ Go has predeclared types, but you can also create your types. We call those types custom types.
◦ For instance, a Booking type can have a method to compute the total price to pay by the customer.
The paper and the digital edition of this book are available here. ×
I also filmed a video course to build a real world project with Go.
4 Predeclared types
Go has several predeclared types. Those types are part of the Go core; you do not need to declare them to use them. We can classify them
into three categories :
• Boolean type
◦ bool
• String type
◦ string
• numeric types
◦ complex64, complex128
5 Composite type
In the previous section,its we have seen that we can create a variable of a basic type.
• arrays
• pointers
• functions
• slices
• maps channels
• struct
• interfaces
2 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
// types/composite/main.go
package main
import "fmt"
func main() {
// array constructed with the basic type uint8
var arr [3]uint8
// struct, interface
// ... see next sections
fmt.Println(arr, myPointer, nameDisplayer, roomNumbers, score, received)
}
[3]uint8, *uint8, func(name, firstname string) string,... are called type literals. Composite types are constructed with type literals
struct {
Name string
Capacity uint8
Rooms uint8
Smoking bool
}
1. Explicitly specified: in this case, the field has a name and a type (in the previous examples all fields are explicit
2. Implicitly specified: in this case, we call those fields embedded fields (see next section)
3 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
Here we create a type from another type. This other type is called the underlying type.
Type definition
4 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
usa := Country{
Name: "United Sates of America",
}
We can create a new element of type Country, without specifying any fields value :
empty := Country{}
usa := Country{
Name: "United Sates of America",
}
Other fields will be equal to the zero value of the type of the field. Here the value of CapitalCity will be equal to the zero value of
strings : "" .
belgium := Country{
"Belgium",
"Bruxelles",
}
Here we create a value of type Country and we set the field Name with “Belgium” and the field CapitalCity with “Bruxelles”.
7.2.0.1 Field values need to be specified in the same order as in the type struct declaration.
5 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
This code will compile, but there is an error. The value of Country.Name will be “Tokyo” (not “Japan”)
When you use this syntax, you should initialize all fields.
The paper and the digital edition of this book are available here. ×
I also filmed a video course to build a real world project with Go.
usa := Country{
Name: "United Sates of America",
}
usa.CapitalCity = "Washington DC"
if usa.Name == "France" {
fmt.Println("we have an error !")
}
9 Embedded fields
6 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
In a type struct, we can add embedded fields. Embedded fields are defined implicitly.
In the type struct Hotel we have an embedded field Country . Country is another type struct.
Embedded fields have no explicit name. The field name is the type name.
Embedded field
Here we embed the type *Country (pointer to an element of type Country ). The field name is the type name : Country :
hotel := Hotel{
Name: "Hotel super luxe",
Country: &Country{Name: "France"},
}
fmt.Println(hotel.Country.Name)
7 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
// types/embedded/main.go
package main
import "fmt"
func main() {
hotel := Hotel{
Name: "Hotel super luxe",
Country: Country{Name: "France"},
}
fmt.Println(hotel.Country.Name)
}
11 Test Yourself
11.1 Questions
1. Give an example of an array type literal.
2. What are the differences between basic types and composite types?
3. In a program, you find the following code : type Switch bool . What is the type name? What is the underlying type?
11.2 Answers
1. Give an example of an array type literal.
1. [123]uint64
2. What are the differences between basic types and composite types?
1. A basic type is predeclared in Go. To use it, you do not have to declare it.
2. A composite type is not predeclared, you can declare it by using a type literal
3. In a program, you find the following code : type Switch bool . What is the type name? What is the underlying type?
2. It is not composite; it is not composed with other types. map[uint8]string is a composite type.
1. T
8 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
The paper and the digital edition of this book are available here. ×
I also filmed a video course to build a real world project with Go.
12 Key Takeaways
• A type is a set of values with operations and methods specific to those values.
◦ array, struct, pointer, function, interface, slice, map, and channel types
• Type structs allow you to group data together with fields. Each field of a struct has a type and a name (an identifier)
• Implicitly: you embed a type into the struct type, the field is then called an “Embedded Fields”
Country is a type struct. It is also a field of the type struct Hotel , it’s an embedded field
• To select a value from a type struct variable, you can use a selector expression, with the character "." .
hotel := Hotel{
Name: "Gopher team hotel",
Country: Country{
Name: "France",
CapitalCity: "Paris",
},
}
log.Println(hotel.Name)
log.Println(hotel.Country.CapitalCity)
1. Go Specification https://golang.org/ref/spec#Types↩
Bibliography
Previous Next
Table of contents
Did you spot an error ? Want to give me feedback ? Here is the feedback page! ×
Newsletter:
Like what you read ? Subscribe to the newsletter.
9 of 10 02/01/2023, 02:07
Types - Practical Go Lessons https://www.practical-go-lessons.com/chap-13-types
Practical Go Lessons
By Maximilien Andile
Copyright (c) 2023
Follow me Contents
Posts
Book
Support the author Video Tutorial
About
The author
Legal Notice
Feedback
Buy paper or digital copy
Terms and Conditions
10 of 10 02/01/2023, 02:07