From the course: Learning Go

Group related values in structs - Go Tutorial

From the course: Learning Go

Group related values in structs

- [Instructor] The struct type in Go is a data structure. Structs are similar in purpose and function to CS structs and Java's classes. They can encapsulate both data and methods but unlike Java Go doesn't have an inheritance model. You don't have concepts like super or sub structs. Each structure is independent with its own fields for data management and optionally its own methods. A struct in Go is a custom type. You can declare your custom types in the same file as the rest of your code, if you have a fairly simple application. To create your own custom type, start with the type key word then give your type of name. I'll call mine Dog, if you name the type with an upper case initial character, it can be used by other parts of the application. In Go vocabulary we say that it's exported, if you use a lowercase initial character it's non exported or private. Now say that this is a data structure, by adding the keyword struct, then put in a pair of braces. Now you can give the struct properties, I'll give this struct a property of breed and I'll say that it's a string. And then I'll also give it one of weight and say that that's an int or integer. Just like the name of the type, the property names are either exported or non exported. And again, it's determined by the initial character. Uppercase means that it's exported, and the rest of the application can read and write to those property values. Now, in my main function, I'm going to add some code to create an instance of that struct. I'll create a variable that I'll name poodle, and I'll give it a value of dog, and I'll set the properties inside a pair of braces. This is sort of like a constructor in other languages but you aren't calling a method, you're defining an object. So use braces around the argument list instead of parentheses. Pass the values to the new object in the order in which they're declared. So I'm going to say my breed is poodle, and I'll give it an age of 10. Then I'll put that in my print line command and I'll see the string representation of the object. Notice this warning indicator, it's just a warning. The code still runs, but I see a message saying that an exported type should have a comment or be unexported. To fix that, I'll add some code above the type declaration I'll press Command slash on Mac or Control slash on Windows to create a single line comment. And then I'll say dog is a struct. I'll save and that warning goes away, and the code still runs. Now, if I change the initial character of dog to lower case and save my changes, visual studio code will display an error and I'm told that the dog type isn't available. I'll switch it back to upper case and save the changes again, and now I'm in good shape. To see a struct field names and values for debugging purposes, use the print F function, and then create a string that looks like this. %+V and then I'll add a line feed at the end. And then I'll say, I'm looking at the poodle object. And now I see all the data from that object. Each value of a struct is technically known as a field just like with classes from other languages you access the struct individual fields with the dot operator. So for example, I could use string interpolation like this. I'll say the dog's breed is, then I'll put in a placeholder and align feed, and then another label weight and another placeholder and another line feed. And then instead of passing the poodle object, I'll pass in the fields. Using poodle.Bread and poodle.Weight. And there's the result. And if I want to change values, I can do that as well. I'll say poodle.Weight, eight=9, my dog has lost some weight, let's say, and I'll copy and paste this bit of code and I'll put the value again, and see that I've successfully changed that value. So those are the basics of how to use a struct to group and store multiple values. And again, there's more you can do with structs to encapsulate functionality in the form of methods. And I'll describe how to do that in the next chapter.

Contents