Structs in Go
This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
You can define a struct and initialise it using following example:
You can dot operator (".") to access the fields in a struct.
Anonymous struct
You can create an anonymous struct for informal uses in Go.
Struct is a value type
Unlike map is a reference type, struct is a value type. This means if you assign one struct to another, the struct will be made a copy and assign to the other. Any modification in one struct won't impact the others.
The result is:
{John Pertwee} {Tom Baker}
Same like array, if you want two struct refer to the same location, you can use pointer to do so.
The result is:
{Tom Baker} &{Tom Baker}
Embedded type of struct
You can embed a struct in another struct by simply declaring the embedding struct in the other. For example:
The result is:
{{Emu Australia} 48 false}
You can also initialise the struct in a structured way, however you can still refer the fields in the embedded struct directly.
The result is:
Emu
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
Comments
Post a Comment