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

The "Count Trees 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 count trees problem.

Preview
Close

Transcript from the "Count Trees Go Solution" Lesson

[00:00:00]
>> Do you guys wanna do Go? You wanna move on? I'll let you guys decide. It's totally okay if you want to, don't feel bad.
>> Yes.
>> Yeah, let's go. Let's go. That was the closest Tom Cruise moment I'll ever have. All right, here we go. I'm not even sure what that actually technically means but you can imagine.

[00:00:18]
All right, p3 let's go and let's do main.go. And so we're gonna do the exact same thing again, package.main. And now we need to create the same enum, right? Let's create the exact same enum that we were doing before. And so we'll do a type Thing = int, const, tree, snow thing equals iota.

[00:00:43]
And of course, get rid of that, there we go. So we now have our enum, tree is 0, snow is 1. We can now do our func getInput, pass back the string and go back to our previous project. Make sure we have this beautiful piece of code right there and redo it, but this time actually copy and paste it.

[00:01:07]
There we go. We have this beautiful piece of code right here. And now we just need to take this, split it by line, and then we can do the exact same thing or we can just look at the individual characters. So first, let's go like this. Func main, do that, brings us into the middle, go lines =, actually we can just go for.

[00:01:27]
We do want our row, remember, we want our row this time. So let's include the index into our getInput array. So for row line = range getInput, right? And then now, that's not quite it, because that's gonna walk over every single character. And you can tell right here, it's a rune or a character, right?

[00:01:46]
That's what they call a character. So we're gonna go strings just like before, Split getInput newline, right? There we go, awesome. So now it's gonna let us know we're not using these two characters, so let's make sure we use them. But now we're gonna go over each single line and what do we need on the outside?

[00:02:09]
We probably need a tree count, right? Let's make sure we have a tree count so we can keep track of this. And do we need a column length? I mean, technically we don't since we have each line right here. We actually already have the column length right there, so we can just call length on that thing and use that as our modular operator instead of doing this little bit of extra bookkeeping right here, right?

[00:02:33]
We don't actually technically need that. Again, another just little technical trick about this, but it works out. So let's do the exact same thing that we were gonna do. Let's take the line, the current line, and now what are we gonna do with this thing? What is this thing?

[00:02:50]
It is a string, so can we get the character? Can we index in our, can we do that? So let's just try it out, right? A is declared not used, awesome, so we can actually use that. So I was right, I must have just been misreading my error.

[00:03:02]
Fantastic, so let's go this times 3, modulo length of line. So unfortunately in Go, it doesn't have a .length property. Remember we always call functions on everything. So length is the function you call. It should be very reminiscent of Python, right? This looks like Python. Even if you don't know Python you kind of actually know a little bit of Python.

[00:03:25]
I always find that I struggle with it but at the same time I can still do things. All right, so now we know this character at this point, which means that we should be able to just go, if this thing equals pound. Well, what do we do here and then of course can't do that, right?

[00:03:43]
So what is this thing asking us? I already know the problem, can convert this thing to a byte is what we're kind of arguing with. So let's go like this. Since we are using ASCII and we know it's ASCII, we can convert this and this should all work out nicely.

[00:03:58]
And so we should be able to go treeCount += 1. Awesome, format, Printf treeCount %v, on those treeCount. Awesome, so theoretically this should all effectively do what we're trying to do. We're just walking this simple array and that is it. So we've kind of reduced the problem quite a bit and it's something actually rather simple.

[00:04:22]
So long as I didn't just completely screw up something, we should be able to go run p3. There you go, never leave room for yourself screwing something up, but there we go, very very simple, right? It was shockingly simple so the problems again, I didn't wanna use too many tricks but it gets progressively easier as you know the tricks to do these problems.

[00:04:43]
And so there you go, very simple, very nice. This thing is just because when you index into a string, you're indexing into a byte because strings are UTF8. And you can imagine that if you have a multi-width character, such as one of the characters in this one right here, this is not an 8-bit character as I assume this is 16-bit character.

[00:05:03]
Therefore, if you index into the fifth byte, what are you gonna get? You're gonna get a half a character, so it's totally screwed up. So that's why it's always dangerous to work with these. Often people use runes as a way to work with them. A rune is the thing that encompasses the entire character.

[00:05:17]
There we go, it sounds really weird, sounds like Runescape, I don't trust it at all. And there we go, so let's go over to Rust and let's solve this last problem using Rust.

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