Lesson Description

The "Creating Go Models" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through building the project models, which are the structures for each entity in the project, such as movies, genres, actors, and users. He also explains how pointers indicate optional fields in the models.

Preview
Close

Transcript from the "Creating Go Models" Lesson

[00:00:00]
>> Maximiliano Firtman: So we will continue creating another package that is gonna be our models. So I'm going to create another package in the root there models. What's a model? We will create the structures for every entity, let's talk about entities for now, that we will have in our project.

[00:00:24]
So what about entities? So let me go back to a slide first, I'm going to close it. So the entities that we will have are actually the movie that's kind of the main entity. So it represents one movie, in the system, in the database. Genre, and why is it a different entity?

[00:00:46]
Because one movie can have several genres, so it can be science fiction and comedy, for example. It's actually pretty simple. It's just maybe ID and name, in the future you may have other things, but just that. Actor, actress, okay. In this case, it has the name, first name, last name, image and so on.

[00:01:09]
And user, we will use the user or the account for logging in, the user, for authentication, okay? So we need to create these models. At least let's start with the first three that they have to do with the movies, okay? So then, we will go and create the model.

[00:01:31]
So I'm going to create one file, genre, oops, .go. Of course it's giving me an error because I don't have the package header, that's why it's giving you an error immediately. Actor.go, and then we have movie.go. And if we add the package models, now it's not giving us Errors, if we don't have any structure yet, any type declaration, but at least we don't get any errors.

[00:02:09]
>> Maximiliano Firtman: Remember, later we will map these models with the data coming from the database, thanks to a repository. So let's start with the simplest one, with genre. So we create a type, we use the name capital G. Remember, if not, we won't be able to use it from the outside.

[00:02:32]
And it's a structure, we have an ID, we typically, it's a pattern. Use the ID with capital I capital D, so all caps for the ID. It's kind of a go pattern and the name. Remember capital, we use title case here, so we can use it outside as well.

[00:02:55]
So that's genre, it's pretty simple. Then you have actor. So we create type, actor is another structure in a3 in the documentation the readme you have this in case you want to copy and paste. But also for our brain to actually get the information and store that information inside our memory it's a good idea to type sometimes.

[00:03:22]
So we have an ID, we have a first name as a string, we have a last name as a string and an image URL. It's also a string, but one of the problems is that the image URL might not be there. Remember, I mentioned that it's optional. So maybe we don't have the picture of every actor.

[00:03:46]
So that's why we are adding an asteric, a pointer there. That means it's nullable or nullable in a Go language. Okay, make sense? And finally, the movie. This is probably the most important one, and the one that has more data. We also have an ID integer. There is a TMDB_ID that maybe I don't need it, but this is because we have on our own idea, remember I mentioned that I created a subset of the full TMDB database.

[00:04:26]
The TMDB database has around, if I remember correctly, 70,000 entries, which is a lot. So I just picked 5,000 and we can still have the idea of the original TMDB in case you need it later. Why you may need that later? Let's talk about this for a second.

[00:04:47]
Let's say you wanna publish this as a real website for movies, okay? So you wanna create the movie database. The problem is that you got the database, and this is static. And of course, we have new movies every week. So, you need to update your database, and TMDB has an API, so you can query latest movies.

[00:05:12]
And how do you know if you already have the movie or not? Well, you use that ID. Okay, you don't use the title because maybe the title of the movie ID can be duplicated, okay, so don't use the title, just use that ID. So we have the ID, we have the title again, so we have a tagline that's something from marketing from the movie.

[00:05:35]
ReleaseYear, Genres, and this is important, this is a collection, not one. Overview, it's a string, but some of these properties might be optional, so I will use a pointer. We have a score. Actually, you know that IMDB, they have a score, users are voting and sending a score, so you have an average.

[00:06:09]
This is not coming from copyright issues, so it's coming from other websites like Rotten Tomatoes and others, so they create an average of user rating, okay? Okay, that's kind of a score. Popularity is also coming from different providers, it might not be there. Then, we have keywords.
>> Maximiliano Firtman: Keywords, this is just a collection of array of strings.

[00:06:37]
Keywords, for example, if a movie is space related, so space, time travel, themes, animals, so things like that. So we have an array of keywords. A language, this is the original language of the movie, so a string. Can be optional in the data. The poster URL, we have the trailer URL, that's the video.

[00:07:09]
And finally, the cast, or the casting, where we use actors, okay? So that's how the movie structure looks like, that's our model.
>> Maximiliano Firtman: Is it mandatory to create the model? Not really. You can just grab the data from the database, convert that into JSON and send that to the client.

[00:07:32]
But the advantage of creating a structure is to mimic the idea of OOP. So we kind of created an object, and then later we can add methods to that object. So it's better to do this, also for security purposes and also for future proof, because what happens if someone is changing the database schema?

[00:07:56]
I mean, we are changing the name of the fields in the database. Well, you're downloading the field as it is. You're conforming that into a JSON and sending that to the client, and the client will give you an error. Well, for error management here, we will know if the structure is matching or not our model.

[00:08:16]
So that's why it's important to have a model here. It feels like a lot of work or more work, but the advantage is that we will get a better future for us. Okay, when we have our models. Any question so far on models?
>> Speaker 2: The data type makes it an optional field, right?

[00:08:40]
The star.
>> Maximiliano Firtman: The star means pointer. It's a pointer to a string, it's a pointer to a float, it's a pointer to whatever. And it means a pointer, from this point of view, it means that it may be nil. So when we create an actor, so let's create here, like a quick actor, like a temporary actor.

[00:09:01]
We create an actor, and that actor may have different properties and different, even I can pass the values here. So when I'm creating actually, this is a I'm in a movie. So let's create a movie, actually, when you're creating the movie. Now, for example, the title is mandatory.

[00:09:22]
You must have a title because it says it's a string, it cannot be nil. For some optional when you have optional values, like they can store new in a database from the database here, it goes to nil. And to do that, we use the asterisk, but having mine, the asterisk by itself doesn't mean it's an optional value, like a new level value.

[00:09:50]
It's a pointer with a lot of things that have to do with pointer, pointer management. If you wanna get deeper into that, check my other course basics of Go at Front End Masters, where we actually get into the pointer idea. Because by default, when you pass in values actually as an argument, it's always passing as a value, so as a copy, unless it's a pointer.

[00:10:17]
So it has to do with that. But for our use case. We can say that the asterisk is for optional attributes, okay, so that means that we can set nil on those attributes as well. Okay, make sense?
>> Speaker 3: Otherwise, if you don't put it, it just if it's a string, it's just an empty string, right?

[00:10:38]
So it will be there, but it will just be there.
>> Maximiliano Firtman: There is a difference from a database point of view between empty string and null, and the same happens here. The problem is that if in the database you have null store as a value for one field, when you're trying to fill your model, it will fail.

[00:11:00]
Because there is no way to store that null unless you have an if you have a condition saying, if it's null in the database, I will save empty string here. Okay, but yeah, it's kind of a hack that you're doing. To actually automatically map your data with the data that is coming from the database you have to add the pointer.

[00:11:21]
So then, if it's null in the database, it will be nil automatically here. But remember that empty strings are not the same as nulls. Also, when you have here, float for the score popularity, it's even more complicated because if it's not no level, what do you store for Node Zero?

[00:11:45]
But maybe zero is a pretty, pretty bad movie, so zero may be an actual value, value. So then you need to start hacking this, and say, minus one, but it's really hack. So it's much better to let the system store the new ability of that body.
>> Speaker 3: Got it, makes sense.

[00:12:06]
>> Maximiliano Firtman: Okay, makes sense?

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