Structs

Now we'll dive into a bit more advanced data structures, structs! A struct is like a slice, except it uses curly brackets { } instead of square brackets [ ]— but most importantly, structs also differ in that they store values with keys (called fields), and can accept several data types. A struct can be highly versatile and can not only be used for data storage, but for more complex algorithms like hashmaps, which we'll go over later. Most interestingly, we can have structs with nested structs within them, and even slices as well! It's important to mention that fields and values have a colon ' : ' separator between them.

Here's a simple example of a struct:

type Person struct {
    Name string
    Age  int
}
thisIsMyStruct := Person{Name: "John", Age: 22}

In slices, we used a process called indexing, and the index notation format to access values, e.g.; mySlice[3]. With structs, we use a similar process called field access, and the field notation format to access values.

The main difference is that we use fields, instead of solely integers that refer to position, to select values from a struct— and not to confuse you, but a field in a struct can also be an integer =).

For example, in thisIsMyStruct above, you'll see the value "John" belongs to the field, "Name". As well as the value 22, belongs to the field, "Age". This is what field notation would look like for accessing a value for a specific field in a struct:

nameValue := thisIsMyStruct.Name
fmt.Println(nameValue)
# Output: "John"

Try printing the "Age" value in the code editor below:

ageValue := thisIsMyStruct.Age
fmt.Println(ageValue)
# Output: 22

Now we'll look over a more intricate struct, that's a bit larger and complex.

type PersonWithLikes struct {
    Name  string
    Age   int
    Likes []string
}
thisIsMyStructWithLikes := PersonWithLikes{
    Name:  "John",
    Age:   22,
    Likes: []string{"Exercise", "Cooking", "Coding"},
}

We see something in this struct that we haven't seen before, a slice as the value to a field. Let's try accessing the 1st index of John's likes using a mix of field notation and index notation!

likes := thisIsMyStructWithLikes.Likes
fmt.Println(likes)
# Output: ["Exercise", "Cooking", "Coding"]
likesFirstIndex := likes[1]
fmt.Println(likesFirstIndex)
# Output: "Cooking"

Quiz

Question 1

What is a struct in Go?





Question 2

How do you declare a struct in Go with the fields "Name" (string) and "Age" (int)?





Question 3

What will be the result of the following code?

type Person struct {
    Name string
    Age  int
}
  thisIsMyStruct := Person{Name: "John", Age: 22}
  fmt.Println(thisIsMyStruct.Name)




Question 4

How do you access the first element of the "Likes" slice in the following struct?

type PersonWithLikes struct {
    Name  string
    Age   int
    Likes []string
}
thisIsMyStructWithLikes := PersonWithLikes{
    Name: "John",
    Age:  22,
    Likes: []string{"Exercise", "Cooking", "Coding"},
}
fmt.Println(thisIsMyStructWithLikes.Likes[0])




Code Editor