Transcript from the "Modifying Struct Values Solution" Lesson
[00:00:00]
>> In exercise 6A, we wanted to modify the describeGroup function to only accept new users represented by the spaceAvailable field of the group, if there are fewer than 2 existing users. So let's hop over into our describeGroup function. And on our group struct, you'll see our spaceAvailable attribute is set to a Boolean.
[00:00:23] So before we spit out this description, I wanna see like, hey, how many users are in this group? Do we need to modify that value, update that struct and then print out the resulting value to the console here? So at the top of our describeGroup function, let's throw in a little if block.
[00:00:38] So we're gonna say if len(g.users) > 2. We wanna change that spaceAvailable value to false. And to modify an attribute, you would dot notation and then set that new value on the right-hand side here. So let's go ahead and print everything to the console right now on line 51, we're describing group g.
[00:01:08] And group g only has, let's bring it back down to 2 users at a time. So if our logic is asking that if it's more than 2, set it to false, right now we only have 2, so we should still see true in our terminal here. If we run it, we see that accepting new users is true.
[00:01:28] If I make a new user here, I'm just gonna copy this guy. I'm not gonna modify the values for now just for demonstration purposes, but I will change the variable name to u3. And if I add a third user to our users slice here, we now do have more users than 2.
[00:01:46] So the length of our group.users is now above 2. So if I run it again, I see that our accepting new users has been changed to false. So cool, everything seems like it's working, everyone's happy. But if we actually just print out what our group looks like, so g is our variable for our instance of group.
[00:02:10] In that describeGroup function, I wanted to permanently modify this value. I wanna say that, hey, if the user group is higher than 2, don't accept new users, spaceAvailable should be false, and that should be a permanent modification. So if I run this now, you'll see that even though that string prints out false, because in the describeGroup function, we've modified it locally, the actual group itself is still printing true for that Boolean.
[00:02:35] So something's happening or we're not modifying that variable in a permanent way. And so an interesting thing about the go language, which is another really fascinating part that differs pretty strongly from JavaScript is the concept of pointers and references. We're gonna talk about that in the next section, where modifying a variable permanently requires a little bit more behavior than simply modifying it in place.