Transcript from the "Custom Types" Lesson
>> Now we're going to be hanging out in the structs.go file for quite a while. We're gonna be building on this and adding a couple more examples. But the one thing I wanna do first is create our second type of a struct. So let's create a struct type of Group.
[00:00:14] Imagine you have a couple different types of users in your application, some of them are admins, some of them are general users. So imagine that you can group each type of user together, and that's what this struct is kind of in charge of. So some of the attributes in my live on Group would be a role, for instance, admin, general, guest, whatever you want.
[00:00:36] And then it's gonna have a collection of users. And so here, if a role is gonna be a string, and we know we want our users to be, Users plural, to be a collection of our user types that we just created, we can use User as a type name.
[00:00:58] So see here, we're saying, okay, we want an unknown number, a slice collection, of User types. A couple other attributes we're gonna add that we'll play with later is a newestUser, which is a User type. And spaceAvailable. So let's say, hypothetically, there's only space available in the Group if there's a certain number of users in it.
[00:01:24] So if there's less than 10, then spaceAvailable is gonna be true. If there's over 10, maybe spaceAvailable is false. So we're gonna play with these in a couple upcoming examples here. Once again, we're getting an error because our capital G Group needs to be commented, since it is exportable.
[00:01:40] And I'm gonna say Group represents a set of users, cool. So now we have these two structs that live in our application, one of which is a User. And then a Group which defines a couple additional things, including a set of users. Let's create some functions that can interact with some of these things.
[00:02:03] So I'm gonna create a first function for describing a User. So let's say, hypothetically, we want to introduce this User in our application, like welcome to the application, so and so, tell us a little bit about yourself. So if I call the function describeUser. I gonna want it to say some stuff about User.
[00:02:28] So similarly to how we can pass strings or other types to functions, you can also pass structs. I'm going to assume that describeUser takes an argument u of a User type. At the end of this function, I'm gonna print back out a string. So within here, all I kinda wanna do is return some type of description.
[00:02:55] So let's do, set a variable desc for description. And then I'm gonna use the Sprintf function, kinda just to practice. But if you'll recall from the beginning of class, Sprintf is the type of formatting that actually stores that string to a variable. So if you recall, print line simply returns the number of bytes.
[00:03:18] And then if there's an error message, Sprintf is going to return the actual string that's going to come into the parentheses on the right-hand side here. And with that f, I'm going to want to interpolate some of those verbs in there, so we can get really specific on what parts of the User we want to describe here.
[00:03:34] So in our Sprintf function, let's just kinda give it, let's see. Kinda list out the attributes in a more readable way. So the Name is gonna be the full name of the User. And let's say we want it to say Marilyn Monroe. Email, particular email address, I think we'll just keep it like that, so email@example.com.
[00:04:13] Obviously, we don't wanna just hard code in these values, cuz all of our users are going to have different information there. So we'll interpolate in those verbs at the end, both of which are strings. So we're going to need three, we're gonna need one for the first name, one for the last name, and one for the email.
[00:04:31] Which actually go into the sentence, so we're gonna switch this up. Marilyn is going to be represented by the first name, and that's its own string on our User here, so %s. The last name is also its own attribute, so we're gonna use a second verb for Monroe.
[00:04:47] And then the Email is a third string and we'll pass that in with a third %s. On the right-hand side, the series of arguments we actually wanna use are the dot notation attributes from the User that we passed in. So the first one, if we want the first name, it'd be u.FirstName, u.LastName, and u.Email.
[00:05:12] Because I want to return a string out of this function, I'll return the description.