Structs
This page is under active revision, content may be updated without notice
Go has a speciel type struct
to define custome type with named fields. Struct act like collection of attributes/properties similar to classes in OOPs languages out there, but not exactly.
Structs
Struct can have named attributes of any type and methods can be defined to operate on that. Lets define a Person
struct that will contain first_name
, last_name
and age
So that’s it, we just defined a person struct with some attributes. Let’s use it. There are many way to initialize an struct, here are some
Note when we initialize it without providing any value, go assign default value (zeros of type assigned) to each attributes, depending on there types, like first_name
, last_name
will be assigned a blank string "" and age will be assigned 0.
This can also be in initialized using new
function that allocates memory and return pointer.
But most used way is to initialize while passing values to it, here is how
Attributes of a struct can be accessed by .
operator. So to access first_name
of p
of type Person
, one can use
Struct with Pointers
Pointers with structs work in same way as they do generally in Go, but Go allows fields to be accessed without explicit de-referencing. For example
Last updated