What are Structs in Golang? The Ultimate Beginner Guide in 2022 with Code Examples

Structs is a collection of fields where you can define the primitive value types of each field. This is also known as “structural typing” or “duck typing”.

Coming from a TypeScript world, this is just a format for me to construct objects in Golang with a strictly type format. If I stray away from the defined structure, the compiler will give me an error message.

Use the quick links to jump straight to the different parts of structs.

  1. How to define a struct?
  2. How do I declare a struct?
  3. How to initialize a struct?
  4. How to pass a struct to a function as a parameter?
  5. How do you access struct values?
  6. How to create nested structs?
  7. How to initialize a struct with nested structs?

We’re going to take a look at what exactly structs are, how to use them, and a lot of code examples. Let’s dive into!

How to define a struct?

To define a new struct in Golang, you’ll use the keyword type and struct.


type Person struct {}

The next step is to add props aka fields, in your struct. So in our Person struct, I’m going to add fields such as name, age, and favorite color.


type Person struct {
	name string
	age int
	favoriteColor string
}

At this point, you have defined a new Struct with fields to it. You can now declare or initialize the struct.

How do I declare a struct?

To declare a struct, you can create a new variable and assign it the type of the newly created struct.


var person1 Person

When I create a variable and assign a type of the struct, Golang will add default values. An empty string ("") for the name & favorite color field and an a zero (0) for the age.

How to initialize a struct?

To initialize a struct, you’ll have to declare the variable with the struct and give it curly braces ({}). Within the curly braces ({}), You’ll need to add values to the fields defined in the struct.


person := Person{name: "John Doe", age: 27, favoriteColor: "blue"}

fmt.Println(person)

Here’s what the output would look like.


{John Doe 27 blue}

How to pass a struct to a function as a parameter?

To pass a struct to a function you’ll need to create an argument that the struct type, and initialize a struct when invoking the function. Let’s look at a code example!

I have created a function called printPerson() that prints the argument variable person.


func printPerson(person Person) {
	fmt.Println(person)
}

Now, I’m going to initialize our Person struct when invoking the printPerson() function.


func main() {
	person := Person{name: "Jack Snow", age: 30, favoriteColor: "red"}

	printPerson(Person{name: "John Doe", age: 27, favoriteColor: "blue"})
	printPerson(person)
}

How do you access struct values?

To access values from your initialized struct you can use a period (.).


person := Person{name: "Nick", age: 25, favoriteColor: "grey"}

// Access name
fmt.Println(person.name)

// Access age
fmt.Println(person.age)

// Access favorite color
fmt.Println(person.favoriteColor)

How to create nested structs?

To add structs within structs, is as simple just adding them to the struct definition. Like the one below:


type Colors struct {
	hair string
	eyes string
}

type Person struct {
	Colors
	name string
	age int
}

I’ve created two structs above, Colors & Person.
Person defines the overall definition such as name, age, hair color, and eye color. Since hair color and eye color are similar, I’ve extracted them and put them in the struct Colors

All I had to then is just add the Colors struct inside the Person struct. Now let’s see a code example that initiates the struct above.

How to initialize a struct with nested structs?

To initialize a struct within a struct–such as Colors–the value must equal the struct name followed by the curly braces ({}) just like how the the variable was created.


person := Person{
	name: "Mr. Peacock",
	age: 38,
	Colors: Colors{
		hair: "purple",
		eyes: "brown",
	},
}

To access the values of a nested struct, you’ll still use the period (.) as we did before without nested structs.


fmt.Println(person.Colors.hair) // purple

I like to tweet about Golang and post helpful code snippets. Follow me there if you would like some too!