Transcript from the "Accessing Nested Structs" Lesson
>> So let's take about three or four minutes and add another function here. So the function is going to be describeGroup. And in the describeGroup function, I'd like the sentence to say something like, this user group has 19 users, the newest user is Joe Smith. Accepting New users is true or false.
[00:00:34] So based on this sentence and the shape of our group struct, create a function that's similar to our described user that prints out the appropriate string. So to start with, let's create a function called describeGroup, that takes a group struct as an argument and returns a string. Within this function, we're going to create a describe variable to return at the end of our function and set that to a similar Sprintf formatted string.
[00:01:03] And within here, if we're building the string that's on line 25, we want that to say, this user group has x amount of users, so %d. The newest user is a first name, and a last name, both of which are strings. And accepting new users will be set to a boolean with %d.
[00:01:28] Then the three arguments that are going to provide values for each of those string verbs will be g. The first one we're trying to calculate here is the number of users in the group. And to do that, we need to get all of the users, so g.users and then find the length since it's a slice.
[00:01:49] So we'll call len on g.users. The second verb is going to be the first and last name of the newest user. And so to access that, if we look at our group struct, our newest user is a user type. And on our user type, we have a First and Last name with capital F and L.
[00:02:09] So down here we're going to say, our g.newestUser.FirstName, and then g.newestUser.LastName. And then our last verb here is going to be whether or not there's space available, so we can say g.spaceAvailable, which will return true or false based on our struct definition up here. So now that we have both our describeUser and describeGroup functions ready to go, let's go and print those out down in our main function, make sure everything looks all right.
[00:02:45] Additionally, because we're returning a string and our describeGroup function, I forgot to make sure we return that at the end of our function here. So I'm going to add return desc for description. And this guy at the end here should be person t, my apologies, person t for boolean.
[00:03:11] Cool, so hopping back to our main function. Let's create an instance of our group. So I'm going to set the variable g to a group struct. And within here we have a few different attributes. The first is a role, let's call this guy admin. Users is going to be a slice of user structs, and then within the curly braces we can throw in a couple initial values.
[00:03:38] Right now we just have one user on line 33, let's copy this guy and make a second one to add to our array, I'm gonna call it u2. Let's update the user ID to be 2, and then modify some of the values For some mock data. Cool, and so within our user slice here, I'm going to pass in u and u2 to instantiate our user slice with two users.
[00:04:16] For our newest user attribute, I'm just going to use the u2 variable. And then spaceAvailable, for now let's set that to true. Cool, underneath this, I just want to print some of these things to the console. So I'm going to Println. DescribeUser and pass it our first user, so just that u variable.
[00:04:43] And then for a second one, let's call the describeGroup function and pass it g for group. For clarity, I'm going to kill these last two Printlns and then in the terminal, let's execute the go run command, and navigate into section six, code/structs.go. So you'll see that both of these descriptions are now printing to the console, having interpolated all of the variables the way they should be.