Thus far we have looked at many of the different data types that Go comes with. While you might be able to get away with making any program using only these types; it would be extremely tedious and difficult. Certain programs reuse data shapes over and over again. Therefore, creating a custom data type will ultimately save tons of time. In this post, we will be looking at Structs and Interfaces in Go.

Structs in Go

Structs are basically a custom data type that contains a set of named fields. In a struct, each field or variable has a type and like with functions we can define multiple fields that share the same type. Consider a Person struct.

structOur Person struct contains a name, gender, and age. Above, you can see the different ways that we can use the custom type that we created with this struct. When we use the new keyword to define a variable of our type struct, we allocate memory for the fields, set each of the fields to their zero values and return a pointer (*Person). We can access fields later in the code using the dot (.) syntax.

Methods in Go

Methods are special functions that we can associate to our structs in Go. We use a special type of syntax to do this. Consider for a moment the creation of a function that prints out our instance of the person struct.

Notice that we are passing the function parameters before the name of the function in this case. This is called using a receiver. The receiver is what allows us to associate this method to the struct Person. It also allows us to call the method with the dot syntax. This type of coding is much easier to read and it allows us to ignore using the & operator. Go will automatically pass a pointer to this function.

Because the function is associated to our struct of person, we can name it something simple. Lets try creating a different type of struct.

In this example, we created a type for a square. We then created a method to calculate the area of the square and then printed that result in our main function. We can also create embedded types using structs. For example, if we wanted to extend our person struct and add a few more fields we could do so.

Notice that we are able to bind our Person struct to a field inside of our Animal struct. We can also just say that it includes person rather then adding a new field to the Animal struct. When we add the struct the second way, we can just access that field by using the type directly with the dot syntax. For example, say a is a new animal type variable, = "Jazzpurr" is how we would assign the field with this syntax. Also, notice that when we embed structs into other structs they inherent the methods as well.

go-methodsInterfaces in Go

Say we have multiple shape structs inside of our program and they all have methods for their areas and parameters. We can further abstract these shape methods and assign them to an interface.

In this example, we have two method names and their return value types. We define an interface in a way that is similar to a struct. However, instead of defining fields inside of the interface, we define a list of methods. These methods are methods that must be contained in a type for that type to be a part of the interface. This interface might not seem very useful, however, we can use it as an argument for a function, a field for a struct, or even to create a struct out of the interface type.
Lets look at an example of these different implementations.

In this example, we created two structs one for rectangles and one for squares. We create an interface of shapes with the area methods inside of it. We use that shape interface to create a sum of areas method. This actually makes our interface behave like a struct in that it allows us to classify a shape as a shape, though not formally. We can then calculate the area of the square and the rectangle inside of the main function and print them out. Keep in mind that this example isn’t the best use of interfaces in Go. It merely shows the types of relationships that we can create between structs, interfaces and methods in Go.


In this post, we looked at both Structs and Interfaces in Go. We also looked at methods and we talked about fields. In our next posts, we will start to look at go-routines and how go handles concurrency and multi threading.