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

The "Type Definitions Q&A" 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 addresses a student's question about defining methods with string types and provides an additional example that demonstrates defining types with an attached method to clarify the purpose of the attribute arguments.

Preview
Close

Transcript from the "Type Definitions Q&A" Lesson

[00:00:00]
>> Yeah, I'm sorry.
>> Can you go back to the types we were just looking at?
>> To there?
>> Yeah, I'm a little bit confused between the thing between the word function and the name of the function.
>> What is this?
>> Yeah, what is that?
>> What is that?

[00:00:15]
>> Yeah, what's it called? What's the word for it?
>> It's not a code, that's actually-
>> It's like a special argument?
>> It's the attribute argument.
>> Yeah.
>> So actually, it's an argument because it's an input variable. Actually, let me create another type that will answer your question.

[00:00:37]
I'm going to create a location. And let's say your location is, it can be a string that has latitude and longitude, comma separated, something like that, right, doesn't matter. And then I can create a function that receives a location and calculates the distance to another location. That was my point.

[00:01:00]
So I would create a function, okay, let's put it here so we can clean this up a little bit. So I have that new type, location, and I'm going to create a function distanceTo that receives another location, okay, like destination, let's call it destination that is of type location as well.

[00:01:20]
And then we have to do some calculations here, okay? And you'll return a distance. So, so far we're going to return 10, okay? That's not the point. So this is an argument, this is a function that I can make it global as well, exported, sorry, not global, exported, with capital D.

[00:01:39]
Now, if I have a locationTest function where I create a location, for example, I say I'm in New York City. So then I create a variable nyc and say it's a location and it receives a string, right, because it's based on a string. So here I will add the coordinates of, something like this, whatever.

[00:02:06]
So I have New Your City, and then can I calculate the distance to other location that I might have? Well, no, why? Because DistanceTo is not a method, it's just a global function at this point. So I should call it without nyc, like this. Now it's complaining because I'm not using nyc.

[00:02:34]
Yeah, it's a global function. So I still miss one part because actually, what I need to do here, we have an origin and a destination. And where is the origin? I have the destination, but where is the origin in this call? It's not there. Yeah, I might receive it as an argument.

[00:02:56]
So you say, yeah, you receive it as an argument. Okay, like that. No, sorry, location. And then say origin, destination, which is okay. It works. So now I need to pass nyc as the first argument. Is this a method? No, it's not a method. This is a function, okay, receiving arguments.

[00:03:18]
If you want to convert it into a method, and why do you wanna do that? Because it's more object oriental, okay? It's nyc.DistanceTo, in fact, I should change the name. If it's a function, it should be distance between, not to. If I wanna use this syntax, a more OOP syntax, then I need to move this into a method.

[00:03:45]
How to do that? One of the arguments, the one that is going to be the subject of the method, will get out of the argument's list, and it will go to this special argument place. And now, you can use it. It's still an input argument, because origin and destination are both inputs.

[00:04:10]
But now, this is the origin, and this is the destination. That's how we work. So it's like picking one argument and move the argument to that special place. And when you do that, you're upgrading that function into a method.
>> That makes sense. I was kind of trying to wrap my head around like, well, what if you need to pass a bunch of stuff in there?

[00:04:35]
>> In this case, there are only one argument, because it's the argument where you're going to hook that method into the type. And we're hooking this into a location.
>> Right, but it could have other arguments-
>> Here you can add more arguments. Yeah, that's why I did these examples.

[00:04:52]
So you can see that you can have more arguments as well. All the arguments are input arguments for the function. So from a function, from the code point of view, it's an input argument. But I was saying that some developers are actually tempted to use this here, why?

[00:05:09]
Because then you treat this as really being Java or JavaScripty, right? Because it's your option. You're creating a method. You have the D subject. Can you? Yes, because this is not a keyword in Go. So you can use it as an identifier for your variable. But yeah, it looks like you don't like Go or something like that.

[00:05:32]
So if you use this, you are not a Go developer, you are a developer using Go and someone is forcing you. So yeah, we typically use a noun that refers to the type that we are adding the method to. So in this case it can be origin or it can be location.

[00:05:49]
By the way, can it be location? It can also, location, location, which is, well, if the type is going to be exported, maybe it's going to have capital L that will maybe change a little bit this, at least on some positions. And now you don't have that problem.

[00:06:08]
No, this is location with lowercase. [LAUGH] Okay, so then you have the identifier and the type, right, but that's also possible.

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