Learn Go: Structs
Learn how to create structs which organize data in the Go language!
StartKey Concepts
Review core concepts you need to learn to master this subject
Structs and Fields
Struct Definition
Struct Instances
Struct Methods
Access Struct Fields
Passing Structs as Pointers
Access Pointer Struct Fields
Arrays of Structs
Structs and Fields
Structs and Fields
In Go, a group of related variables can be defined as a struct. Each variable within a struct is known as a field.
Structs in Go
Lesson 1 of 1
- 1We often have many variables related to each other, but managing using them all together can be a headache. Luckily, Go provides us with a way to group several variables into one custom data type. …
- 2We’ve just learned the importance of structs in grouping complex data types. But how do we define the structs we’ll use in our programs? In this exercise, we will introduce the syntax for defining …
- 3We’ve defined our structs, but how do we create variables of that type? To use a struct we just defined, we have to create an instance of it. Assume we defined Point from the last exercise. We coul…
- 4We’ve defined our struct and created an instance, it’s time to use them. In this exercise, we will explore how to access and modify a struct’s variables. Let’s say we have an instance of the Stud…
- 5We can use functions to capture logic involving our structs and simplify it. Structs will often have important operations that can be performed on them. For example, with a struct representing a …
- 6Without pointers, when a variable is passed into a function, only a copy of it is used inside the function. We can use pointers to modify values in our structs within a function. But how do we get …
- 7What can we do when dealing with many structs of the same type? We can use them in an array together! Let’s say we want to create an array of the following points: {1, 1} {7, 27} {12, 7} {9, 25} W…
- 8When we have complex groups of fields in our structs, they can be combined into their own struct! For example, in the Employee struct, we have two separate fields for the first and last name of the…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory