22. Structs
Working with different types together
Struct Type
Slice and array are good for storing collection of data which is of same type. What if our data is made up of smaller types which are different? This is where struct comes in handy.A struct is a type that contains named fields. Struct is a value that is constructed out of other values of many different types.
Slices and maps are used to store collection of similar data where as struct is used to groups together zero or more named values of arbitrary types as a single entity. Grouping together data of different types, that's where struct is useful.
Declaring Struct
A simple struct can be constructed using struct
keyword as
struct {field type}
name
and age
are called fields of struct.
Generally, we will use multiple instance of struct in code. It is very common define struct as user defined type using type
keyword for reuse.
now person
is a user defined struct
type. p1
and p2
variables are both of type person
.
Creating struct
Declaring a variable of a particular struct type creates a struct. There is no explicit call required to any other function.
Accessing Struct Fields
Struct fields are accessed using dot operator .
This can be used to read as well as write to struct fields.
Zero Values
When a struct is declared, it gets initialized to zero values for its all the fields.
Struct literals
We can use struct literals to declare and initialize struct with initial value instead of zero value. We can define person struct type using struct literal as
Comparing structs
Two structs of same type are comparable if and only if all the fields of the struct are comparable using ==.
Struct as another struct field
Like we can use data types like integers ,floats and strings as fields for a struct, we can also user another struct as field for building new structs. This is useful for reusing already created user defined types.
Note that if we need to access name of superman we need to use superman.human.name
Anonymous fields for struct
What if we wish to use superman.name
instead of superman.human.name
in above code? Go supports this by providing type embedding. An inner struct that is stored within an outer struct using an anonymous field is said to be embedded within the outer struct.
We are embedding human
type inside superHero
type. There is no named field inside superHero
to which we are assigning human
type. Outside struct literal, we can access fields of human
type directly as if these fields exists on superHero
. This is called type promotion.
Embedding Types
Blog Post
Last updated