Check out a free preview of the full Basics of Go course
The "Serving from Data Structures" 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 responds to student inquiries about the required imports and clarifies whether the dot in the template represents a type. Additionally, the segment includes a demonstration of serving a museum exhibition from a data structure.
Transcript from the "Serving from Data Structures" Lesson
[00:00:00]
>> You may have already mentioned this, can you scroll up really quick?
>> Where?
>> For your imports.
>> Imports, yeah.
>> So I have this too, text/template, and then there's also html/template. Did we want html/template for this since we're passing in the html?
>> No, for these is same, but it's interesting.
[00:00:21]
Yeah, it's the same, should work anyway. Yeah.
>> Is dot in template a kind of type?
>> Is dot kind of type? So no, dot is a reference to the object, to the value that you're sending as the second argument of your execute. So it's not the type, it's actually the value.
[00:00:43]
In this case, it's a string, so it looks weird. Most of the time you won't be passing a value, a number or a string, you will be passing an object or a slice, so then the dot makes more sense. I will do it right now, actually that's the next step.
[00:01:01]
So the next step is to create. We're going to create the data structure for serving all these exhibitions from this museum. Right now, they are actually in JSON. So if you go in the public folder, in gallery we have data.json, that's the JSON. We could read the JSON and use our marshal solutions to have it back, that's one option.
[00:01:27]
We can do that because we have already been playing with JSON. We can just create like static for now, structure in Go with that information. Remember it can come from a database, of course there are libraries not within Go but external libraries to connect to MySQL, SQLite, SQL Server, Oracle, other no SQL databases as well, MongoDB.
[00:01:53]
So you can connect with most of the stuff by using libraries. So I will create though another package now. In fact, how many package do I have right now in my app? Only one, main. Okay, so be careful, not every folder is a package, that was the tricky part of the question.
[00:02:15]
So let's create a data folder, and within here I can create an exhibitions.go file for the package data. What happens if you copy and paste the JSON and try to convert it into a normal Go based structure? It works, it's not actually a big deal, we can do that.
[00:02:39]
Remember, we can read the JSON it's not a big deal. Or just because this is kind of boring we can go to GitHub, and I already have that for you. github.com Frontend Go Fundamentals. In femmuseum we already have on data exhibitions.go the list, we can just take that part only.
[00:03:06]
It's just the JSON but, With, Let's say, Go format. And what's exhibition? Exhibition is the type that we need to create, I don't have that type yet. So I can also copy and paste it, but let's write it, that's useful. So I'm going to create the new type, exhibition, that is going to be a structure with a title string, description string, and image string, okay.
[00:03:38]
It looks like JSON, the only problem is that we don't use here quotes. That if you paste the JSON, the JSON has quotes for the key names or the property names, so you need to go and remove them one by one. And also some weird thing that I'm not sure you will like is that the trailing comma is mandatory.
[00:04:04]
So if you try to remove the trailing commas, you can see that image has a trailing comma even if there is no other property. And also there is a trailing comma at the end of the collection, it's mandatory. So you always need to have a trailing comma, does anyone think on a reason of why they did that this way?
[00:04:27]
>> At least the reason we did it at my last job was so that if someone added other things afterward it wouldn't count that line as a line with a change in GitHub.
>> Yeah, that's one reason. Also because just to avoid the problem of forgetting the comma and giving you an error, so simplicity.
[00:04:49]
Remember the three goals of Go, make things simple, to execute fast, and combine fast, this is simplicity. It looks weird the first time because yeah, I don't have any property so why I need a trailing comma? Well, yeah, you need a trailing comma, just in case in the future.
[00:05:02]
Because we know even if you don't think that you will add a property, in the future there is a good chance that you will add a property to that structure, okay. Cool, so it's complaining because we are not using the list yet, which is okay. And this is list with lowercase l, I did this on purpose, that means it's not exported.
[00:05:25]
Just to show you that we can implement the pattern of, for example, creating a getter. So I can create a function GetAll that returns a slice of exhibition values and I'm going to return the list. Similar to what you do in Java or C# when you have a private property and then you create a getter and a setter.
[00:05:49]
Does it make sense? Remember this is the constructor of a slice of exhibition. The constructor angle is always curly braces, so it's not actually a square brackets as an array in JSON, a reminder. We have the type first and then we open curly braces, and that's the constructor of types in Go.
[00:06:18]
Okay, so now we have the data. Actually what I want, going back to main.go, is instead of sending test I wanna send something from data. We're going to start simple, and what we're going to do is to talk with data. That's our package that we need to import, so when I select return, I press return, it's adding me the import.
[00:06:39]
We are going to Get All, and I'm going to say sub-zero. So I'm going to start passing only the first element, or it doesn't need to be the first, we can sub one if you want, so any one. Just one element, let's try first with one element. So now the template is receiving as data source one of these exhibition options, values.
[00:07:06]
So now if you go to the template file dot, it's not going to work, it will show me the full contents of that object. What I need to use here, well first let me remove that. And now I need to replace h2, instead of that Life in Ancient Greek reference I'm going to use double curly braces and use dot and the property name.
[00:07:34]
Being the property name, title, okay, dot title. And now the dot makes more sense because that standalone dot wasn't kind of interesting, okay. Because most of the time you are passing a value that is a struct, so and then you use dot and the name of the property.
[00:07:54]
Of course, we are going to do the same on the paragraph. So I'm going to remove all the contents from that p element and I'm going to say .Description. And for the image, I think that in the data we don't have the image forward slash, it's just the file name.
[00:08:12]
So I can just go and replace only after the forward slash, can I do that within an attribute in HTML? Yes, because the template is not actually reading HTML. This is different from the Angular template. In the Angular template, Angular knows if you are in a text node or if you are in an attribute, okay.
[00:08:34]
Now everything is a string. We are just working with the strings here, so you can just replace things at any moment. If this works, let's see, I need to stop my server, run the server again and try it. To refresh template, I'm going to see Aristotle with the right image.
[00:09:08]
And that's actually embedded in the HTML, it's not JavaScript. We've removed the JavaScript, it's actually embedded in the HTML. This is server-side render.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops