Basics of Go

Struct Methods

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Struct Methods" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates creating a data structure and explains how to import and access the structure data in a different package. It is important to make the properties of the data structure public in order to access them.


Transcript from the "Struct Methods" Lesson

>> So before seeing interfaces that is the last part that it's actually pretty simple, let's create a new display with this. So I'm going to create another version, so, again, we know the deal open folder. This is going to read let's say front end masters, FEM, or front end math FEM.

And we know the deal, it's first go mod init and a name whatever. And may not go with package main, package main, and function main, it's always the same, we always start in the same way, okay? And we are going to start working with creating our data structure.

So let's say we wanna create the database later or something like that or the JSON base structure. And so we are going to create the classes of our project, it's going to be simple but just to play with a couple of features that we have to understand how this work, okay?

So for that I will create a new package, remember how to create the package a folder? So we're going to folder we can call it data, or model if you want, we don't have one unique design pattern here. And do we need any special file within the package?

No, just go files with any name, remember, names are not important. We're just going to call this, for example, we can start creating the data structure for instructor, so I'm the instructor of the course. So we're going to create the course with instructor, not a big deal, nothing fancy, it's a big package data, that's the first part, okay?

So, we're going to create the instructor, so we wanna do something like this. Okay, class instructor but no, this is not go, we don't create classes, so what we are going to create is a type. The type is going to be called Instructor like so, capital I will be better, we wanna export this.

Are there use cases to make it private? Yes, just if you have some data types that are only internal to the backlash, it's fine, right? But it's not common, then the struct keyword and calibrates. And here we'll say something like, okay, has an integer, has an id, it's a name, first name can be first name, last name, okay, something like that.

No score can be an integer and we can say that for now we're good. It will complain, okay, by the way, why is it complaining? Because, yeah, we are not using it, but it says, every time we create a structure, it will complain. Well, there is an explanation of why it's complaining, that I will re-factor this in a minute, so I will wait to see if we can find why it's complaining.

So let's say I wanna go to main.go now and use that class, it's not a class, it's a structure, remember, struct. So first, to use that, I need to use data, okay, data, return, it adds my import, remember the import has a prefix of my modules name, and then the folder.

Data and you can change that with an alias if you wanna change that, if not data the last part of the that URLs kind of package name will be the object name. And so then how to create one of those objects? So I gave it a variable, I wanna create an instructor, Max, that's me, so what's the type?

Well, the type it should be data.instructor, do I need the data prefix? Yes because if I don't have it, it doesn't know where that thing is coming from, so I need data.Instructor. For every type if it's from another package, yep. You could create an alias of that type locally in your package, that weighs to work with that, but typically there's no need.

So, and how do you create one, okay? In fact, instead of using var, what if I wanna use this? I need to construct 1, okay, so how do you construct 1? To construct one, we call the constructor, how's the constructor? Just curly braces and of course it's complaining because I'm not using the value, for now, let's just print this just to avoid errors.

Because sometimes you don't know if, mostly when you're learning Go, you don't know if it's a real syntax issue until you go and see it or the anxiety issue from Go. So that's actually equivalent to what you are typically used to the new instructor. Is it the only way to that?

No, there is also a new keyword available, also I make that we will see later. But we don't typically use them for standard situation, which has used this, but it's not like new like that, it's more a function, okay. It's actually a function, something like this, but we don't use it, okay?

It's there, you can also use it, but it's not what we typically use. So we use this other constructor, and in the constructor, I can also work with the properties, give me a second for that. Let's say when I set my name, so it says my name is Maximiliano, try to type that fast, right?

It's not working, why?
>> Cuz its first name? It was first name, that's why, but isn't working either, why? Any you guess what's going on? I got read the problem, it says first name and define, I said, no, but we do have a first name.
>> Cuz you didn't create a constructor?

>> No, in fact, you don't create constructors, constructors are created by default. We can create factories, that is a design pattern, I will show you in a minute, that's not the problem, it's just that it doesn't exist. Hint, you know the answer, I'm adding more pressure on you.

You know the answer.
>> We need to make a method.
>> No, you don't need to make, I mean, you can create getter or a setter, can you create a setter? Yeah, is it necessary? No.
>> Lowercase letter.
>> Lowercase letter? It's private, that's a private property, yeah, that's how it work, I need these properties capitalized if I want to use them from the outside.

I'm sorry to disappoint you like this, so now I can use the property and now also I can use a constructor. When I was doing this, it wasn't suggesting me anything because we didn't have that public property. But now I can I can define the properties here, like in JSON, remember?

So I can set my last name, here, also, in the constructor, I can set one or all the properties in the constructor or non properties. The constructor is created automatically, I cannot change it, I cannot replace it, it is what it is. Okay, make sense? So now you know, is it possible to have private properties?

Yes, like on ROP, sometimes you want private property, maybe the ID is private and you can only get it from a function, from a method, okay? So how to add a method to it? For example, if I wanna print the instructor details, I'm gonna create the pretty print like a nice print of the instructor.

Well, I create the method, how? Within the type? No, outside of the type, like a global thing, in fact, it can be on a different file. The only important part is that it should be in the same package I mean, don't do this at home. But I can create methods go on the same package, and I can add method on a different file because the important part is to be in the same package.

I will change it, but just to prove that that works, how do you create the method? It's a function with this special input argument first, of type instructor. I will call this I and print, that responds with a string, a strong, no, a string. Well, maybe the instructor is strong but when I'm here, I'm gonna call fmt fprintf, I'm going to return that.

And I will say the format string is going to be a value for the first name, or a value with the last name, a comma, the first name, in parentheses, the score, whatever. The score was a number, so let's use d for decimal digit, so then we take the instructor dot last name, the instructor dot first name.

Be careful because here the order is important, can you change the order? You can, don't try, don't do that, right? But here you can say that here you wanna use the first argument, things like that but now, there is no need for that, you just set this in order.

Because they are in the same package, I can do that, so now I can print Max print but it's not working, why? Why? Capital P, Capital P, if not it's not available, get used to that. And now if I run this, go run dot, I say Ferroviaria and NEMA zero, okay, as an instructor that's my score right now, yeah.

We should add an emotion Ukraine emotion when they give you a zero. Okay, it doesn't make sense, kind of? You can see that working with capital letters is one of the things that you will need to train your brain, so it takes time to get used to that.

That's a problem, you will see that a lot initially until you get used to that, okay, make sense? Any question?
>> Can I see the instructor guide again
>> Yeah sure And by the way, was it me that created all this space that is nicely tabulated? No, when you save, in fact you can change this a little bit, like so, when you save it's reformatting the file for you.

You can disable that if you want, it's part of the plugin settings. Again, I know that typically you set the methods on the same file, and by the way, the import should go at the top, Like so, so yeah, this probably makes more sense, but it's not mandatory, I mean, it's up to you.

Remember, everything in the same package goes to the same bag, it doesn't matter which file it is. So that's how you set structures with properties and methods.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now