Skip to content

Struct and Interface Types in Go

Posted on:April 26, 2020 at 10:40 PM

This is the fifth entry of my weekly series Learning Go. Last week I covered a few more pieces of the Slice and Map type. This week I will be talking about the Struct and Interface types.

Struct

A struct is a data structure that allows you to compose values of different types. Because of that, a struct is a great way to aggregate data. From a computer science perspective, a struct in Go is considered a composite data type.

This simply means that this is a data type which can be constructed using the language’s primitive data types (string, int, etc), or other composite types. Let’s see one in action.

In this example I will be creating a struct with primitive data types:

package main

import (
	"fmt"
)

type car struct {
	model string
	color string
	year  int
}

func main() {
	c := car{
		model: "tacoma",
		color: "white",
		year:  2020,
	}
	fmt.Println(c)
	// {tacoma white 2020}
}

In the example above, I am creating a new struct of type car.

Anonymous struct

Like many things in programming, there is more than one way to do something. The same can be said about creating a struct. If you are wanting to use a struct for a specific scope, there is a short-hand way to declare them.

package main

import "fmt"

func main() {
	c := struct {
		model string
		color string
		year  int
	}{
		model: "tacoma",
		color: "white",
		year:  2020,
	}
	fmt.Println(c)
	// {tacoma white 2020}
}

Let me walk you through what is happening in this example:

Important note: you must place a comma after each entry in a struct, or you will get an error from the compiler that looks a little bit like this:

syntax error: unexpected newline, expecting comma or }

Method sets

Methods are used heavily in programming and that is no different in Go. Thinking in terms of traditional Object Oriented paradigms, a method is defined and called in relation to the Class it was defined in.

In Go, a type may have a method associated with it, most commonly with a struct. Let’s take a look at an example using a method of a struct type:

package main

import (
	"fmt"
)

type toyota struct {
	model string
	color string
	year  int
}

func (t toyota) start() {
	fmt.Println("vroom vroom")
}

func main() {
	t := toyota{
		model: "tacoma",
		color: "white",
		year:  2020,
	}
	t.start()
	// vroom vroom
}
package main

import (
	"fmt"
)

type toyota struct {
	model string
	color string
	year  int
}

func (t toyota) start() {
	fmt.Println("Hey! I'm a ", t.color, t.year, t.model)
}

func main() {
	t := toyota{
		model: "tacoma",
		color: "white",
		year:  2020,
	}
	t.start()
	// Hey I'm a white 2020 tacoma
}

This example is identical to the previous; however, the change to note here is what is happening inside of the start method.

Interfaces

An interface is both a type and how you name a group of methods in Go. Let’s jump right into an example to explain:

package main

import (
	"fmt"
)

type car interface {
	start() string
}

type toyota struct {
	model string
}
type subaru struct {
	model string
}

func (t toyota) start() string {
	return t.model
}

func (s subaru) start() string {
	return s.model
}

func getModel(c car) {
	fmt.Println(c.start())
}

func main() {
	t := toyota{model: "tacoma"}
	s := subaru{model: "forester"}

	getModel(t)
	// tacoma
	getModel(s)
	// forester
}

In Summary

There are so many ways to optimize and organize your code in Go.

The struct data type helps us compartmentalize our code by common values and allows us to aggregate values of multiple types, all under one type. How cool is that?

While struct allows us to group data creatively, interface allows us to group functionality between our struct values. Thus allowing our code to have a deeper reach throughout our codebase. Now, creating methods that can run functionality across multiple struct types is a painless exercise.

Next week I will be sharing my experience with functions in Go, see you then!

Never miss a post, subscribe to my Substack!