Structs in Golang

Durna Suleymanova
PASHA Bank
Published in
4 min readApr 9, 2020

Hello, in this article, I will explain struct or structure in Golang.

First of all, let’s determine what is struct?

Struct is user defined type which contains a collection of named filed/properties. It is used to collect related data together to form a single unit instead of preserving each of them as separate types. For example, let’s take a person who has firstName, lastName, and age. Collecting these data’s into Person struct is more convenient rather than separating them into different types.

Declaring a struct

Struct can be declared as below.

type Person struct {
firstName string
lastName string
age int
}

This code expresses that I have created a new type named Person which is a struct. It contains 3 fields which located between curly braces. Each field has a name and a type. Fields with same type can be written in one line.

type Person struct {
firstName, lastName string
age int
}

There is another way of creating struct which leads to creating a struct without declaring new type. These type of structures are called anonymous structures.

var person = struct {
firstName, lastName string
age int
}

Creating a struct

Now, I have created a person who has a struct type Customer.

If I run the code above the result will be {0} because I have defined a variable person but have not initialized it. This is the zero value of the struct which shows all fields remain their 0 value.

0 value of string is “”, and for int 0.

Initializing a struct

There are several way of initializing a struct.

We can initialize a struct using struct literal:

var person = Person{"Durna", "Suleymanova", 21}

It’s essential to pass field values in the same order in which they are declared in struct. If there is missing value we will face compiler error.

var person = Person{"Durna"} //too few values in Customer literal

We can initialize a struct based on name:value syntax. We can mix order of fields while using this syntax.

var person = Person{lastName:"Suleymanova",firstName:"Durna",age:21}

Also, we can initialize a subset of fields, the rest will set to their 0 value according to data type.

var person = Person{firstName:"Durna",age:21}// lastName: ""var person1 = Person{}// lastName: "", firstName: "", age:0

Complete code:

Accessing fields of a struct

We can use . (dot) operator for getting and setting fields of a struct.

Pointer to a struct

Creating a pointer which points to the value of a struct saves a step to create a struct variable and creating a pointer to this variable.

person1 is a pointer to the struct Person. (*person1).fistName is a syntax for getting the value of firstName, but Go gives us an opportunity for accessing field without dereference (*person1).firstName.

We can use new() function to create an instance of a struct. The new() function allocates enough memory for all the struct fields, sets each of them to their zero value and returns a pointer to the newly allocated struct .

Anonymous fields

We can declare a struct that has only types without field names. Because anonymous fields do not have field names their types can be expressed as names.

Nested struct

Struct field can be any type of data type. It can also be another struct. In this case, the struct field will be called a parent struct has a struct value. If you would access a field of a struct inside a struct you can use struct.structField.field. Let’s see an example.

In above example, we have 2 structs Customer and Account. “account” field in customer struct is a parent struct. When we create customer struct, we have to initialize values of both Customer and Account.

Promoted fields

As we have learned above it is also possible to define a struct type without declaring the field name, language will define its name from the field type. We can apply this approach to the nested structs. Go will set struct type as a field name:

In contrast to the previous example we removed account field name and just used Account struct type to create the anonymous field. After that we can use . (dot) for get and set the values of the Account struct fields.

When we use anonymous nested struct, all the nested struct fields will be available on parent struct. This is called field promotion.

That’s all for this article. I hope it will be useful.

--

--