Let’s GO! Part 7: Aggregate Types 1: Structs

Ali Azizjahan
3 min readDec 21, 2021
Structs are basically some different gophers together!

Overview

Aggregate types, as the name suggests itself, are basically some other variables with different types that come together under one name. Structs are one of the aggregate types in GO. They’re basically consisting of some fields that can have different types and come together to form a record. If you’re familiar with Javascript and Typescript, you can think about structs as something close to Objects and Object Types in Typescript, but not literally them as the mindset of GO is somehow different.

What Are Them? No, Seriously!

Structs are basically the types that let you define your own types in GO. They have keys and values just like Objects in Javascript. The keys are just names of the variables and values can have their own separated types. Let’s say that structs are some variables that can have different types and they come together under one name, and that is basically true. They’re here to prevent us from defining a lot of variables that are meant to be related. For example all of the information about a person (e.g. Name, Age, Gender etc.). Are you thinking about nested structs?! Me too! It’s possible; no worries. But we won’t be doing that so often. Let’s see them in action.

The Syntax

The syntax for defining struct types is like this:

type MyStruct struct {
FirstVariable TYPE
SecondVariable TYPE
ThirdVariable TYPE
...
}

What’s happening up there is basically this:

  • That type is the keyword to define types in GO.
  • That struct is the keyword that comes after your struct’s name to let GO know that you’re defining an struct type.
  • Those TYPE can be whichever type you want. Either the Basic Types that you already learned in the previous part, or struct types or even other types that you’ll learn in the next parts. Just like normal variables and constants.

Now you may wonder why did I use PascalCase in the names instead of camelCase that we were using when defining simple variables and constants. Of course you can also use camelCase when defining structs and their members and PascalCase when defining variables and constants, but there’s a big difference which we’ll talk about it later on when we reach the part about Scopes.

Let’s see the real code:

The output should be:

My name is Ali Azizjahan, I'm 23 years old and my gender is Male.
My name is Ali Azizjahan, I'm 23 years old and my gender is Male.
Hi Ali Azizjahan! You look too young for someone who is 23! BTW are you sure you're a male?!

Some notes about the above code:

  • At line 26, this is the shorthand syntax for initializing a variable with an struct type. You can also use the long and verbose syntax like normal variables: var me Person but if you do this then you have to give values to the struct members one by one like this: me.FirstName = "Ali", me.LastName = “Azizjahan” etc. which makes no sense since we’re using structs to lessen the code we write and make it cleaner and easier to read.
  • At line 40, we’re using a function called ToLower that comes from the strings built-in package. The function converts all of the characters inside a string into lowercase with preserving the unicode characters.

Note: Structs are mutable as long as you don’t poke the types or the key names. Now what do you think will happen if we change some of the values of me that is passed to the greetMe function inside of the function? You can try it in your own editor and see what happens but we’ll talk about this too as we reach the part about Scopes.

Aight this is basically the structs! Good job so far.

Dancing gopher that is happy because it now knows structs

Let’s see what are Arrays in GO next.

Next:

Previous:

List Index:

--

--

Ali Azizjahan

Software Engineer and Computers Researcher. Teaching you the things that I wish I knew myself sooner, or I could be taught way easier. https://linktr.ee/kyxey