Check out a free preview of the full Go for JavaScript Developers course:
The "Pointers Solution" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna live codes the solution to the exercise.

Get Unlimited Access Now

Transcript from the "Pointers Solution" Lesson

>> Okay, so circling back to exercise 7a. I'm going to grab this code here. And in my code directory, I'm just gonna create a new file called exercise7a.go. And I'll paste in the boilerplate code from the test itself. Let's go ahead and add a comment here, so our linter is appeased.

[00:00:30] It's real descriptive, cool. So the first thing we wanted to do, if we circle back to our exercise, is define an instance of a user struct, which you'll notice we have our struct definition here, but we haven't created an instance of it. So we'll go into, we'll stick it outside of main.

[00:00:49] For now we're going to say var u is equal to a user. We'll give it some fields. ID will magically be one, let's set a first name to, I'm gonna be a little boring, we'll stick with Marilyn Monroe for right now. Last name set to Monroe, email, she's into Gmail these days.

[00:01:15] Cool. So we have our instance of our user struct. And the next thing we want to do is create a function called update email that takes in that pointer of a user type. So right above main. Trying to update the email. And it's going to take a u, and that's going to be a user pointer.

[00:01:51] Within our function here, we're going to set u.Email. = to " I'm going to print a line while we're in here that just says, we are in update email to know that we're kind of local to this function, and what is our u.Email now Finally back in Main.

[00:02:23] Let's call our function, so we're going to call updateEmail and send in our user, And then fmt.Printline our Updated User, and u. Right off the bat, you'll notice that I am expecting this function to accept a user pointer, which means I can't just give it a regular struct.

[00:02:54] We need to hand it, the ampersand to give it that address since that's what it's expecting. A reminder that the asterisk next to the type name indicates the address itself. An asterisk next to the variable name, here, would indicate that actual value. And then kind of recapping from the end of the last lesson we talked about, I don't have to do star u here, because we are working with a struct and that's that loophole that lets us kind of just like skip that step, a little bit.

[00:03:24] And that is from our chapter seven example. So let's hop into seven pointers code and then exercise7a.go. So you'll notice that in updateEmail, we've modified it and then we print out that user one more time after the fact, you'll see that the email has been permanently updated.