What is a type?
What are predeclared types?
What are composite types?
What is a type literal.
What is a type struct?
What are embedded fields?
How to create a type.
Type
Value set
Predeclared types
Type struct
Composite types
Fields
Embedded fields
“A type determines a set of values together with operations and methods specific to those values.”1. Let’s decompose this definition :
\nA set of values.…
\nA 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
... with operation and methods specific to those values. Operations and methods are capabilities that are shipped with values of a type.
\nTypes 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.
You can define methods that are attached to a type.
For instance, a Booking type can have a method to compute the total price to pay by the customer.
NOTE : we will cover them in a dedicated method
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 :
\nBoolean type
\nString type
\nnumeric types
\nuint, uint8, uint32, uint64
int, int8, int32, int64
float, float32, float64
complex64, complex128
var rooms uint8 = 130\nvar hotelName string = "New Golang Hotel"\nvar vacancies bool
\n\nIn the previous section,its we have seen that we can create a variable of a basic type.
\nYou can use those basic types to construct composite types.
\narrays
pointers
functions
slices
maps channels
struct
interfaces
// types/composite/main.go\npackage main\n\nimport "fmt"\n\nfunc main() {\n // array constructed with the basic type uint8\n var arr [3]uint8\n\n // pointer constructed with the basic type uint8\n var myPointer *uint8\n\n // function constructed with the basic type string\n var nameDisplayer func(name, firstname string) string\n\n // slices constructed with the basic type uint8\n var roomNumbers []uint8\n\n // maps constructed with the basic types uint8 and string\n var score map[string]uint8\n\n // channel constructed with the basic type bool\n var received chan<- bool\n\n // struct, interface\n // ... see next sections\n fmt.Println(arr, myPointer, nameDisplayer, roomNumbers, score, received)\n}
\n[3]uint8, *uint8, func(name, firstname string) string,...
are called type literals. Composite types are constructed with type literals
A struct type is a composite type
\nstruct {\n Name string\n Capacity uint8\n Rooms uint8\n Smoking bool\n}
\nA struct is composed of fields. Fields can :
\nExplicitly specified: in this case, the field has a name and a type (in the previous examples all fields are explicit
Implicitly specified: in this case, we call those fields embedded fields (see next section)
We can declare new types based on existing, predeclared types.
\n// a new type Firstname\n// underlying type : string\ntype Firstname string\n\n// a new type Currency\n// underlying type is string\ntype Currency string\n\n// a new type VATRate\n// underlying type is float64\ntype VATRate float64
\nHere we create a type from another type. This other type is called the underlying type.
\ntype Country string
\nWe create a new type Country
The type name is Country
(identifier)
The underlying type of Country
is string
.
You can also declare a new type with an underlying composite type.
\n// new type "ExchangeRate"\n// underlying type is map[string]float64\n// map[string]float64 is a type litteral\ntype ExchangeRate map[string]float64\n\n\n// new type "Birthdate"\n// underlying type : time.Time (type Time from the time package)\ntype Birthdate time.Time\n\n\n// new type "Hotel"\n// underlying type : struct\ntype Hotel struct {\n Name string\n Capacity uint8\n Rooms uint8\n Smoking bool\n}\n\n// new type "Country"\n// underlying type : struct\ntype Country struct {\n Name string\n CapitalCity string\n}
\n\nfrance := Country{\n Name: "France",\n CapitalCity: "Paris",\n}\n\nusa := Country{\n Name: "United Sates of America",\n}
\nWe create two variables of type Country
: france
and usa
.
We can create a new element of type Country, without specifying any fields value :
\nempty := Country{}
\nYou can also specify certain fields :
\nusa := Country{\n Name: "United Sates of America",\n}
\nOther 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 : \"\"
.
In the previous example, we write the field name then its value. You can omit the field names :
\nbelgium := Country{\n "Belgium",\n "Bruxelles",\n}
\nHere we create a value of type Country
and we set the field Name
with “Belgium” and the field CapitalCity
with “Bruxelles”.
This syntax has to be used carefully.
\n\ntype Country struct {\n Name string\n CapitalCity string\n}\njapan := Country{\n "Tokyo",\n "Japan",\n}
\nThis code will compile, but there is an error. The value of Country.Name will be “Tokyo” (not “Japan”)
\n\n// WILL NOT COMPILE\nchina := Country{\n "China",\n}
\nWhen you use this syntax, you should initialize all fields.
\n\n// WILL NOT COMPILE\ngreece := Country{\n Name: "Greece",\n "Athens",\n}
\n\nTo access the value of a field, use the character \".\"
.
usa := Country{\n Name: "United Sates of America",\n}\nusa.CapitalCity = "Washington DC"
\nYou can, of course, also access the value of a specific field :
\nif usa.Name == "France" {\n fmt.Println("we have an error !")\n}
\n\nIn a type struct, we can add embedded fields. Embedded fields are defined implicitly.
\ntype Hotel struct {\n Name string\n Capacity uint8\n Rooms uint8\n Smoking bool\n Country\n}\n\ntype Country struct {\n Name string\n CapitalCity string\n}
\nIn 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.
\nIn the preceding section, we have seen that we can embed a type into a type struct. We can also embed a pointer type into a type struct :
\ntype Hotel struct {\n Name string\n *Country\n}\n\ntype Country struct {\n Name string\n CapitalCity string\n}
\nHere we embed the type *Country
(pointer to an element of type Country
). The field name is the type name : Country
:
hotel := Hotel{\n Name: "Hotel super luxe",\n Country: &Country{Name: "France"},\n}\nfmt.Println(hotel.Country.Name)
\nThe name of the field is also Country
.
The embedded field name will be it’s type. Let’s take an example :
\n// types/embedded/main.go\npackage main\n\nimport "fmt"\n\ntype Hotel struct {\n Name string\n Country\n}\n\ntype Country struct {\n Name string\n CapitalCity string\n}\n\nfunc main() {\n hotel := Hotel{\n Name: "Hotel super luxe",\n Country: Country{Name: "France"},\n }\n fmt.Println(hotel.Country.Name)\n}
\nHere the type struct Hotel
has two fields :
One explicit field : Name
(of type string)
And an implicit, embedded field : Country
The name of the embedded field is its type name.
\n\nGive an example of an array type literal.
What are the differences between basic types and composite types?
In a program, you find the following code : type Switch bool
. What is the type name? What is the underlying type?
uint8
is a composite type. True or False?
What is the name of an embedded field of type T ? of type *T?
Give an example of an array type literal.
\n[123]uint64
What are the differences between basic types and composite types?
\nA basic type is predeclared in Go. To use it, you do not have to declare it.
A composite type is not predeclared, you can declare it by using a type literal
In a program, you find the following code : type Switch bool
. What is the type name? What is the underlying type?
Type name is Switch
The underlying type is bool
uint8
is a composite type. True or False?
False. uint8 is a predeclared simple type.
It is not composite; it is not composed with other types.map[uint8]string
is a composite type.
What is the name of an embedded field of type T ? of type *T?
\nA type is a set of values with operations and methods specific to those values.
\nGo predeclares basic types that you can use to create composite types
Composite types are constructed with type literals
Composite types are:
\nType structs allow you to group data together with fields. Each field of a struct has a type and a name (an identifier)
We can specify fields of a struct explicitly or implicitly.
Implicitly: you embed a type into the struct type, the field is then called an “Embedded Fields”
type Country struct {\n Name string\n CapitalCity string\n}\n\ntype Hotel struct {\n Name string\n Country\n}
\nName
is a field specified explicitly
Country
is a type struct. It is also a field of the type struct Hotel
, it’s an embedded field
\".\"
.hotel := Hotel{\n Name: "Gopher team hotel",\n Country: Country{\n Name: "France",\n CapitalCity: "Paris",\n },\n}\n\nlog.Println(hotel.Name)\nlog.Println(hotel.Country.CapitalCity)
\nGo Specification https://golang.org/ref/spec#Types↩︎
Previous
\n\t\t\t\t\t\t\t\t\tPackage Initialization
\n\t\t\t\t\t\t\t\tNext
\n\t\t\t\t\t\t\t\t\tMethods
\n\t\t\t\t\t\t\t\t