Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We've seen that Go can store integers, floating-point numbers, strings, booleans, and more. But suppose we had one integer that represents a number of minutes, and another that represents a number of hours? How do you tell those two apart? How do you avoid accidentally storing the hours integer in the minutes integer, or vice-versa? Go lets you create custom types that will help you avoid these sort of mix-ups.
- Type declaration
- Defines new named type that has same underlying type as an existing type https://golang.org/ref/spec#Type_declarations
package main
import "fmt"
type Minutes int
type Hours int
type Weight float64
type Title string
type Answer bool
func main() {
minutes := Minutes(37)
hours := Hours(2)
weight := Weight(945.7)
name := Title("The Matrix")
answer := Answer(true)
fmt.Println(minutes, hours, weight, name, answer)
}
- Underlying type determines intrinsic operations it supports
-
type Minutes int
will support all the operationsint
does: addition, subtraction, etc. -
type Title string
will support the operationsstring
does, like concatenation of other strings.
-
minutes += 3
- Can also be compared to a value of same named type, or value of same underlying type
if weight > Weight(907.3) {
fmt.Println("Capacity exceeded")
}
if weight > 907.3 {
fmt.Println("Capacity exceeded")
}
- Cannot be compared to values of different type
// ERROR
if minutes > hours {
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up