Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Fissures Go Solution" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through the Go solution to the Fissures problem. This solution uses pointers, so there is no inlining, but there is a string function that can be implemented to include inlining.

Preview
Close

Transcript from the "Fissures Go Solution" Lesson

[00:00:00]
>> Are you ready to move on to go? Yeah, awesome. Let's move on to the go. So we're gonna do the exact same thing is that I'm gonna go to my go package. I'm gonna go in the command folder and I'm gonna create a p2. In here I'll do my main.go package.main just like we are all kind of used to right?

[00:00:20]
All right so let's do the exact same thing I'm going to hop back to my previous project. I am going to find my nice input here. I'm gonna yap that is what that's called. So in vim if you go Y-A-P it actually yanks a paragraph of code or contiguous code and if I do a space it yanks it to of course my clipboard beautiful luck.

[00:00:40]
Use tmux paste it in we're looking good. All right so func getInput exact same thing right we don't pretty much have to change anything with go we don't say function we say funk. You don't use colons to denote when the types coming in you just simply say the type, simple right awesome.

[00:00:58]
All right, so let's do the exact same thing. I'm gonna go type point struct, right. And then from here we'll do x int, y int awesome. Now let's do type Line struct which is gonna have p1 point up I just added a, don't do that doesn't like him there we go we just did it right there right we just put it all in we're creating the exact same thing again but with go.

[00:01:28]
So now let's go funk parsePoint and we're going to take in a line that's gonna be a string. And we're gonna return out a point. But of course remember we have error, so in this one do we want to handle the errors? Or do we want to just simply error program?

[00:01:43]
I'll let you guys decide.
>> Handle.
>> Handle, you guys wanna handle? Okay, so let's handle our errors. Now remember, as I told you earlier, when we handle our errors, lets always return a pointer to the struct and then return an error as our second argument. A beautiful thing about go multiple return values I remember I was doing an interview at Facebook a few years back, and I made such a fool of myself because all I was doing was go during that period as just learning the language for the first time.

[00:02:10]
And the interview was in JavaScript, and the person was just like, well, how do you return multiple values from a function JavaScript? I was like, you can't, cuz I'm in the go world where there's literal multiple values and the person like, yes, you can, no, you can't, you can return one value, that's it.

[00:02:25]
I'm sitting here arguing over semantics, they were right past each other. So one problem about doing a lot of languages is you end up forgetting or you argue over in you're both saying the same thing it's funny. Anyways so let's parse the point. So we're gonna go like this line or we're gonna go I always do this parts that's I don't know why it's always been kind of a word I've used.

[00:02:47]
Again strings has friendly operations for strings and let's do Split passing the string we want to split on because remember they don't have those methods on the strings they have this nice little library and let's do a what is it? Yeah, let's do a little comma right here.

[00:03:04]
Fantastic. So now we have x and y, so we're gonna go like this x, err equals string convert remember, exact same thing this converts the string Atoi let's pass in parts 0. Now we need to handle the err so what I'm going to do is I have my nice little convenient function if error does not equal nil, meaning there's an error here, then return nothing and the err.

[00:03:27]
So my since I did this nice little pointer right here that means I can return nothing I don't have to create anything it's fantastic. Then I'm just going to copy this entire thing paste it in hit it with a y increment that number, again there we go fantastic.

[00:03:43]
By the way for those that are wondering how did you just increment that number in this beautiful program called vim if you hit CTRL A it actually increments the next occurring number on your line it's pretty fantastic I use it all the time. There we go so now I have x and y they're both gonna be numbers we've handled the air conditioning and so now we can return point.

[00:04:00]
Remember do not specify them as a list initializer, use the more explicit form or else you're gonna be a sad panda when you screw it all up and so now we need to return nil for error, there is no error. And of course we have to turn our point into a pointer because that's the reason why we can return nil up here is because pointers can be nil right that makes sense.

[00:04:24]
And of course you can always execute a go format and it puts this thing right here as they once said that no one is happy with go formatting which is why everyone's happy it's the way no one would do it. And so I don't like that but you should always use go for matter to do your code.

[00:04:40]
There you go anyways have this beautiful piece of code we've parsed the point. Now let's parse the line. So I'm gonna like this funk parse line, I bet you a bit a lot of you can start writing this without even me doing it because it's that simple at this point.

[00:04:52]
We'll do that of course, what are we returning a pointer to a line and then an error. Now, we need to do the exact same stuff as we did up here, so we can literally just grab that line bring it down, paste it in and then do what?

[00:05:05]
Well, instead of that, we do a dash, there we go. We've now parsed out each point so we now have a right hand in the left hand and we're gonna do the exact same thing again, p1, err parsePoint, parts, 0, what do we do again? Everyone's favorite thing err does not equal nil, return nil and the err.

[00:05:31]
Awesome, I'm gonna highlight it. I'm gonna paste it. I'm gonna increment it, and boom, we have ourselves part twoawesome. Now I'll return line as a pointer and p1 as p1 and p2 as p2. So we have this final problem right here right which is what just happened right here well, we specified the points as pointers, right?

[00:05:58]
When we returned it, but our line did not take pointer so we have a couple ways we could solve this. But I'd say the easiest way is just to simply say, hey, a line consists of two points and their pointers. For you it doesn't make any difference, right, this is not like C++ where you have to use that caret function to access members, right?

[00:06:16]
During just regular programming you don't really notice the difference between a pointer and a non pointer. The reason why pointers are very important, I'll go over here after we finish this problem section, why it's so important. But for now, this just makes it easy, right, there we go, we've solved our problem.

[00:06:31]
When I hop back down here, you will see everything is fantastic, awesome. So now all we need to do is create the main function do what we did before and we should be good to go. So func main, I'm gonna bring this up, cuz it's annoying to be at the bottom of the screen and we will do of course our getInput and then we'll do strings split just like we did before.

[00:06:56]
What we've been doing is that we just got done converting line from just two points to references, two points, pointers to points. And so now when we return points as pointers from parsePoint because there's an error, it just keeps on flowing through nicely. If you don't use pointers with errors, you run to all those weird things where you're creating these empty structs and everything.

[00:07:17]
This makes 100 times more sense, just a lot more pointers in your lifetime but what does that practically mean for you? Virtually nothing, it just means it just programming doesn't change because go does not have different ways to access members if it is a pointer versus a non pointer.

[00:07:32]
So anyways, so let's continue on right here. We're gonna take our input, we're going to split it on the new line, now we can have this we can call this thing parts if we really want to or lines. But what we can also do, which I think is probably the nicer way, exactly like this, for, we don't care about the index for line in range of this thing.

[00:07:52]
There we go so same thing we did last time, but now we just kind of inlined it. A little bit nicer probably feels a little bit easier and so now I'm gonna create one last thing I'm going to call something lines the reason why I did this for this exact reason is I'm going to create an array of line pointers.

[00:08:10]
Here we go and so in go of course, raise put the brackets in the beginning, not at the end, why? I don't know cries and difference is just a part of it all. So there we go and this of course is just like, hey, I'm an initializer of lines, but there's nothing there.

[00:08:24]
So we just created a empty or empty array, if you will. So now, I have a line and now I want to parse out that line. So I'm running out of the word because I have lines I have line I'll call that an L and then call this thing a line, line err parseline, right?

[00:08:42]
And passing the L and there we go we now have the pointer line out and the err. So if I see that that's a pointer line, if I look at that, that's an err. Awesome, I can handle the err but of course, we're now in the main function.

[00:08:54]
So we can't use this we can't just return nil error. Here I can use something like log fatal for now just say, hey, we couldn't parse the line. And then even if we wanted to, we could even include the line as well, that make it nice and easy, but we'll just do that because it's not gonna fail.

[00:09:12]
We cut and paste this code, it should be all fantastic. One last thing that's a little bit weird about go is that if you use the LSP it actually comes with this as you can see right there. Use a pen to bank, if you accept that what it's going to do is it convert your line into this lines equals append lines.

[00:09:28]
And it's kind of one of those weird things about go, is when you append to a list, it actually hands you back a new list. And effectively things can be resized, things can be moved, things can be different. So it's just as this and this is an easy way for you to always just have the correct thing.

[00:09:45]
So if you just append you don't actually you're not gonna get what you think you're gonna get and so I would just stay like this. There's some reasoning why, how it's represented memory, all that but it's really irrelevant at this moment. And so there we go, we have that.

[00:10:00]
And now of course, we use the standard format print again, someone's angry about the fact that I have both log and print in the same file. We're just gonna be okay with this and we're gonna go printf plus v and pass in lines. And now do the whole go run p2 main and what we should see are these beautiful beautiful numbers we have a pointer which is very unfortunate so you don't get the nice inlining.

[00:10:24]
If we didn't have that then we'd have something nice so let's just jump up here go to the pointer remove that dereference that pointer rerun it and look at how beautiful that is. But again the points are pointers so you don't get a very beautiful printing so that's one downfall of course to using pointers is that the standard format print is not as nice.

[00:10:45]
There is actually a string function that you can implement that will do all this for you so long as you implement the string method and just use a percent V. I believe it should print it out how you specify which makes it a little bit nicer. So there you go we saw that and go, fantastic.

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