This course has been updated! We now recommend you take the Basics of Go course.

Check out a free preview of the full Go for JavaScript Developers course:
The "Structs" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna introduces structs in Go and demonstrates how to define a type on a struct, how to instantiate a struct using a struct literal, and how to access fields on a struct with dot notation.

Get Unlimited Access Now

Transcript from the "Structs" Lesson

>> So now we're gonna get into some of the more fun complex pieces of go that are particularly interesting. The first one of being a struct, which we'll be using a lot in some of our code examples down the road. So we're talking about a struct. A struct is going to define the attributes that live on a particular type that we define ourselves.

[00:00:15] So so far, we've been working with types that are like strings, booleans, integers but you'll notice when we're talking about a struct type, we get to name it whatever we want it to be. And then define the attributes that make it what it is. So in this example case, we're calling the type struct a name of user.

[00:00:31] And the user struct has four attributes on it right now. It has an ID, which is an integer, a first name, last name, and an email all of which are strings. Similarly to passing in arguments and kind of consolidating code, Go allows us to list all attributes that have the same type on the same line and add that type to the end.

[00:00:50] So we've got now two lines with the ID and then all the strings are on the second line. And then to actually instantiate an instance of that struct type, we would use the type name at the beginning of the variable definition. And then the opening curly braces to actually assign values to each of those fields.

[00:01:13] So we're setting the variable name you to a value of a struct user type with values for the ID of one first name Marilyn Monroe, etc. Cool, let's hop over to structs.go and play around a little bit. So now we are moving on into section six, And structs.go.

[00:01:38] One thing you'll notice that's slightly different from the slide is that the attributes in the user struct here are all capitalized. And something that is gonna stay consistent throughout Go at the moment is any attribute that is capitalized will be accessible outside of this package. So even if it's on a struct, if you import this capital U user struct into another file under the package.

[00:02:00] The only attributes that will be visible are capital letters. So I'm gonna switch everything to be capital for the rest of the exercise. So general theme is kinda capital letter means exportable and visible elsewhere. Cool. If I comment out some of this code, let's print this to the console and see what it looks like.

[00:02:33] We still need the struct definition, my apologies. So so far in this file, we've defined our struct using the consolidated syntax for all of our string types around the second line. And then in main, all I'm doing is instantiating an instance of our struct, which is a user and then printing that to the console.

[00:02:53] So if we call go run in section six. Let me back out of our utils file here. Instead of changing into it, I am trying to run it. So I'm gonna modify that to be, go run. You'll see that we have something that looks very similar to an object that prints out all of the values of our struct attributes, ID of one, Marilyn Monroe, etc.

[00:03:29] So similar to accessing attributes on an object in JavaScript, if I just wanted to access let's say that user's first name or last name, we can use .notation. So I'm gonna print out u, which represent our user and then capital F, FirstName. Save this and run it here.

[00:03:49] You'll see the first thing I see is that string printed out as Marilyn. And my second output here is that entire user as a struct value.