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

The "Type Definitions" 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 showcases the declaration of new types, the addition of methods to types, and referencing types using aliases. However, it is important to note that methods cannot be attached to type aliases.

Preview
Close

Transcript from the "Type Definitions" Lesson

[00:00:00]
>> Okay, so let's go back a little bit to slides and let's move into the next hot topic, type definitions. Because so far I mentioned that a package can include constants, a package can include variables, it can use functions. We have already seen those three, and also getting to types.

[00:00:22]
And now it's time to talk about type declarations. So first, we're going to start with the simplest types that you can make. You can create new types based on other types, or aliases to those types. Let's see how that works. To create a type alias, you type the name you want equals, and the object, the target of that alias, or you can create a new type based on.

[00:00:54]
And the only difference here is if you have equals or not, so be careful. It changes everything, okay? So we're going to see this in action. For now, I will keep playing in this code, but just for playing a little bit, and then we'll create another project to understand types better.

[00:01:14]
So we have a data folder here, so I can use that data. I'm put now some go files here. So I'm going to create types.go. That's the file name needs to have that name. No, file names are custom names, so any name. The only problem is that it has to have package data.

[00:01:34]
And then, I'm going to define a type. Let's start with an alias. I would say integer equals to int. So now, you can create the variable x of type integer. Is just an alias, it's just that. Why do you want that? It doesn't feel like interesting, but sometimes you have some.

[00:01:57]
For example, when you have slices, you can say array of strings and or a slice of strings. And say okay it's this. And I know it still doesn't feel so complicated, but then you can say Jason, and Jason you say it's a map with strings as key because they have strings.

[00:02:24]
And as a value, let's say for now it's the json of a strings only. So and then use json as a type, it's an alias. You are not changing the nature of that type. It's just an alias, okay? But if you remove the equals, it seems the same, but it's not the same.

[00:02:45]
It's creating a new type, and why is that useful? Typically is to create semantics. For example, one typical example is we create a distance type. I say, okay, the distance is a float 32 or 64, it doesn't matter. You can even express a semantic on that. For example, it's a distance in miles or kilometers, or meters and yards.

[00:03:11]
You define the union, right? So now every time you use distance as a type, you know what it is. It's not just a float that you're on doesn't have semantics, okay? It has more information, more semantic information is still afloat. Yeah, it is. So the nature is still afloat.

[00:03:31]
But now, you are adding more semantics to that type. And what is important now is that, now when you are creating a variable, let me create a function here to test this. I can create the variable d and if I say something like this, so what's the type of d it's complaining because I'm not using d, but what's the type?

[00:03:53]
Is it distance? No, it's going to be float. But what if, I say something like this? And because it's a type, it has its own converter. So now, it's creating a distance with that value. Okay, what's the distance and number? And you understand better what's this, what's the advantage of this in two minutes, but for now do you have any question on creating your own types like this?

[00:04:33]
You create a new type based on other type. No, okay. You're waiting for those two minutes, right? Those are methods. In fact, let's get rid of the slide and do this directly. So what's the point on this? I can add methods to my type. So for example, let's say it's in miles, I could do this.

[00:04:56]
I can say, okay, let's create the function with the name two kilometers. Okay, but that's a function. Yeah, I can receive a distance. I will refactor this. Disclaimer, I receive a distance in miles. So let's call these miles and then I need to return. Do you know how to make the calculation?

[00:05:22]
No, no one, come on.
>> 6, 2.
>> 1 mile kilometers. We can ask, of course, ChatGPT to do this for us, right? But yeah, we just need to.
>> Maybe it'll be right.
>> Maybe, yeah, you're right, because it's our numbers. So first of all, I need to return something.

[00:05:40]
So I need to say what I'm going to return. What will it be another distance? What if I'm setting that this is in miles? No, but I can add another. I can create another type that is distance in kilometers, but it's also flow 64, and then to return one of those I can convert, This multiplied by distance like so.

[00:06:10]
We do have a problem. What's the problem? We have a type problem.
>> It should be miles.
>> It says it's miles. Yeah, you're right. It's miles, not distant because that's the identifier goes first. It seems like it's working. Yeah, it's working. Is that the method? No, it's a function, okay?

[00:06:33]
It's a normal function. It's a global function, in fact, it cannot export this. I need to use capital T, if I wanna export it. We know that, but it's a normal function. Nothing fancy here. What's going on? Well, actually, I can change this. Can you see this miles, distance declaration?

[00:06:51]
I can take it from the arguments and convert that into a special argument that, brace yourself for the syntax, g oes between the func keyword and the function name, okay? It's not a return type. We open parentheses here and we add that there. So what is this? Is this a return type?

[00:07:17]
No, the return type is still this one. Is this an argument? No, the arguments are here. It's a special argument, and this is actually a method. What is a method that now, I can take my distance and use the dot syntax to convert that into kilometers? Okay, well, what?

[00:07:40]
So this is my variable distance of type distance. Well, I added a method, think about if it's a class, like adding a method to the distance class, but it's not the class. So I'm creating a method and this is how here you select In which type you're injecting that method.

[00:08:04]
This is similar in JavaScript to prototype, when you're adding functions to the prototype of a type. In C-sharp, we do have something similar as well, when you can inject methods. Extension method, I think that's a name. You can also do this on Swift, on Objective-C. So there are many languages where you can do this, you can add methods to other types.

[00:08:27]
So then, do we have two kilometers? So now this is distance, this is kilometers, so that's my variable. Do I have two kilometres? No, because that's a different type. I could add two miles to go back. So in that case, I integrate another method two miles, that will parallely so this right, so I need to put it here because it's the division, or the order is important here and it's not kilometer.

[00:08:59]
I need to return distance, right? And I don't wanna apply this to the distance type. I need to apply this to the other type, and let's call that kilometers, not miles. Does it make sense? So here, you define to which type you're going to inject something. So you can create methods for the type that you have just created.

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