TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
✓
Angular
0%
✓
Astro
0%
✓
Svelte
0%
✓
Vue.js
0%
✓
Other
0%
✓
I only use React
0%
✓
I don't use JavaScript
0%
Go

What Is a Go Struct and How Do You Write One?

Do you have a bunch of Golang variables all related somehow but with different data types? Collect them all together in a handy Go struct.
Jun 19th, 2024 5:00am by
Featued image for: What Is a Go Struct and How Do You Write One?
Feature image by Alicja from Pixabay.

In the Go programming language, a struct (aka “structure”) is a composite data type that makes it possible to group values of different types into a single entity. Structs are handy when you want to group data into a single unity, instead of having to declare separate values.

For example, you could create a struct that collects information on a person like first name, last name and age. You could declare them as individual items, like so:


Now, imagine you have to declare a large number of variables. Your code could quickly become a mess and far from efficient to read/understand.

Instead, you could employ a struct to bring those related variables together.

There are a few points you should understand about structs:

  • Structs are defined using the type keyword.
  • Fields are accessible using the dot operator followed by the field name.
  • Structs are initialized by providing values for each field in the order they are defined.
  • You can define methods on structs to perform operations on or provide specific behavior to the struct.
  • You can compose a struct with other structs.
  • You can either pass a struct to a function or assign it to a new variable, both of which create a copy of the struct. To avoid this, you can use pointers to structs.

First, let me show you how to create a struct. Our example will be named Employee and will contain firstName, lastName, age and pay. Remember, we start by defining the struct with the type keyword, which looks like this:


Next, we add our fields, which are firstName (string), lastName (string), age (int) and pay (int). That looks like this:


The struct looks like this:


Now, we’ll create a main function that will specify data for our struct using field names. This is a good way to start because there’s a parallel that’s easy to follow. Let’s define employee1 like this:


We could also define the struct without using the field names. This method is faster and cleaner but for those new to Go, it’s less obvious. This method would look like so:


What’s important about the above line is the fields must be in the same order as they were declared in the struct. That is the other complication with this type of struct definition. If you were to omit a field or place fields in the wrong order, it could wreak havoc on the app or the output of the app. Because of this, it’s probably best if you stick with the field name definition.

We’ve essentially done the same thing, only one method is quicker.

Let’s roll this struct into an app that will print out the information about our employee. The app looks like this:


Run the above application and the results will be:

Employee 1 details: {Olivia Nightingale 31 1000}

We could also define a second employee. Let’s do it without using field names. That code looks like this:


Run the above application and the results will be:

Employee 1 details: {Olivia Nightingale 31 1000}
Employee 2 details: {Nathan Gage 32 900}

Anonymous structs

You can also declare structs without first creating a new data type. Remember how, in the above struct, we first created the struct with type Employee struct and then defined the struct with employee1 := Employee? With anonymous structs, we can combine those into a single function. Let’s define a third employee in this manner. First, we define employee3 with:


You should see where this is going. In the next lines, add the fields like so:


So far, this looks like so:


We then jump right into defining the fields like this:


The entire anonymous struct looks like this:


The entire application looks like this:


As you probably expect, the output of the app looks like this:

Employee 1 details: {Olivia Nightingale 31 1000}
Employee 2 details: {Nathan Gage 32 900}
Employee 3 details: {Aaron Kennedy 40 1100}

Accessing Individual Fields

One final point about structs is that it’s possible to access only a single field. Remember how our output was kind of nondescript? What if we wanted to make it a bit more detailed? We can do that from within the fmt.Println section by accessing individual fields for each printed line. This can be done like so:


Now, our output is more structured and looks like this:

First Name: Olivia
Last Name: Nightingale
Age: 31
Pay: 1000

And that, my friends, is how you create a struct in Go. This handy feature can help not only keep your code cleaner and more flexible but also easier to write.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.