Basics of Go


Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

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

The "Embedding" 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 type embedding, allowing the parent type to inherit the properties of the embedded type. The segment also covers accessing the embedded data in a factory function.


Transcript from the "Embedding" Lesson

>> Okay, so far we've seen structures, we've seen that we have a couple of times to use capital letters. And although those were like the bad news, we'll see how to add methods, how to define methods to these types that we are creating. How to use a constructor, how to create a factory.

And now let me give you some kind of a good news now. So let's say that now here in front end masters we have like 2 kind of products. You have workshops and courses and then a workshop is converted into a course. So let's say you want to create another data structure for a workshop.

This is a kind of moment where you will start missing. We'll be right so that first package data, because you say hey yeah I need to, I would like to inherit from and maybe add some property or modify some things. Such as, for example, the workshop has the same property, has an instructor the name, but also has a date because it's live, okay?

So you say, yeah, I'm expecting something like creating a type name workshop, the capital W extending from, okay? But yeah, no, this is Go so we won't have, it's an structure and we have to deal with this. Should I copy and paste? We know that copy and paste is not a good idea.

So what options do we have? We do have options. Well, one option is to just create the property here with the course. So we can create a course of type course. Which, yeah, and then add the date. By the way, let's use capital D. Do we have any type for the date?

We don't have the date time, but there is a time standard library package with a time type. And from that type, we have methods to get set, and get the date in different formats, and we can add dates. So, that's the type that we use for dates on time.

Yeah, it works. So, then when you are creating, let's create, for example, here. Now and workshop on Swift Forever, that I did a workshop on Swift so I'm going to create a swift Workshop that it's going to be data. Use column on equal, so the shortcuts will create a variable data.wokrshop.

It's a workshop, okay? And the workshop has two properties course and the date. And the course, we know it's a data course and we can pass all the data. I'm going to refactor this just for you to know. But will it work? It should. So this is the swift for iOS workshop and maybe we can set the instructor as max.

So, and then we have the other property comma for the date. And we can see that we have a now, for example and then we can add some days or whatever. We can say now and that's all. So can we do that? Yeah, we should work. And then I can print f, that workshop.

Again, capital W, and then Swift Workshop. The problem is that, yeah, Swift Workshop is not implementing that string method, okay? So remember, the string method that we created four cores, so it's not working here that's why it's giving me an error that swiftWS it's not suitable for Printf because we don't have it yet.

So I mean it works. It's like a complex structure. We have a workshop with a course, but we feel like we don't like it, right? I mean, it's a solution, but it doesn't feel like the right solution. Do we have a better solution? Yes, we do. It's called embedding.

With embedding, the only thing that I'm going to do is I'm going to remove here, the identifier, okay, you can see that we have the identifier and the type, we will remove the identifier and just keep the type like that. When I'm doing this, this isn't merely an embedding one type into another type, okay?

So where I'm embedding the course within the workshop, so now that workshop, now my constructor shouldn't work anymore. Let me start this constructor again. Now when I'm creating a workshop, so that's an empty workshop. The workshop has, look at the properties, instructor, ID, name, legacy, why? Because it's embedding all the other properties.

It's embedding all the properties from course. Is it extending from? Not, but close enough. So, no, but close enough. All the properties from course are now part of the workshop. Okay, so I didn't need to copy and paste, I just embedded the type within other type. And do we have the output also, we'll see.

Well, first, let me clean this up a little bit. Also, what about the constructor? The only thing is that in the constructor, we don't have those properties. That's a disadvantage. So we have only the direct properties such as course. We still have the course, okay? So I cannot say the name of the course directly in the constructor, for that it's better to create the factory.

Remember factories, so you can create the function here, new workshop. That receives the name of the workshop. And maybe the instructor, should I capitalize these names here?
>> It's a type? Then yes.
>> The type, is this one.
>> Yeah, that should be it.
>> But I'm talking about maybe no the identifiers.

No, those are local variables. You can if you want, but it won't make any difference. So an argument is a local variable to the function. So, no need for capitalizing those, so I'm going to return a workshop, so I'm going to create a variable or I can say like W.

That's a workshop, it's a new workshop and I'm going to return that W and then I can set the name as name. And the instructor as the instructor. So now I have a better way to construct the workshop quickly. By the way, what happens if the workshop also has a name?

We know the course has a name. Now we are embedding the course into the workshop. And now we have two names. We have a problem, right? Does it make sense? What will happen? Well, in this case, when you access the name, it will be the workshop's name and not the course name.

If you want the course name, you still have the property with the name of the type available. Okay, make sense? Do you wanna make things more complicated? You don't need to type this, okay? Because I'm going to delete that. But let's say I create another type that is called, in fact, no, let's do this.

I'm going to also embed an instructor. So, can you embed more than one? Yes. And now the workshop has all the properties from the course, all the properties from the instructor, and also a name, but let's delete this name and also the date, but the instructor and course both have names.

Okay, which is a problem. In that case you will work with the embedding property, .instructor or .course, you will need to disambiguate that part. That it make sense? So, this is kind of inheriting from several classes, being C++ you can do that. This case you have different types, and maybe, you have naming collisions, so the same name of property..

When that happens, you will need to express okay for that property, you will need to say which subtype do you want? Which embedded type do you wanna use?
>> Can you selectively embed the type like only take certain properties from it?
>> Only some properties, no. For that case, you should create a structure of types where you have a base type with only a few, and then you have the other types that are embedding that one, adding some, or adding some others.

That's how you do that. Okay, so now I can use my data.NewWorkshop. This is iOS with Swift. Swift with iOS, max instructor. By the way, I have a type of here, it's not w, it's v. So now we have a question. Remember that when we were on printing the course, okay, well we're bringing the course we were using this method.

Remember we replaced the standard output when that method were for the workshop or not because we don't have it here. Will it work or not? Well, we can test this in action so first I have what's going on here, there we are. So let's run this and the answer is, yes that's good news.

So when you are embedding types, you're also embedding the methods of those types. Okay, because we can we can do multiple embedding that may be a problem as well, okay? So because when you have when you're in main two types, and they have the same method with the same name.

That can lead to some issues, or ambiguity, okay? But I think that's pretty cool. Because at the end, this looks like inheritance, kind of, okay? Kind of. So at the end, it wasn't so bad, we can apply some techniques from the OOP paradigm.

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