In Go (Golang), a struct is a composite data type that allows you to group together related values of different types.
If you’ve programmed in other programming languages before, you may be familiar with the concept of classes in object-oriented-programming. These classes serve as a “template” or “blueprint” to represent just about any object. These objects can then, in turn, be instantiated with different properties or methods.
Go doesn’t actually provide classes, but it does provide a somewhat similar implementation in structs, which enables you to define your own complex types with multiple fields.
Syntax
Unlike arrays or slices, which are homogenous, structs can contain heterogeneous data. Structs have the following signature:
type <name> struct {
// field1 type
// field2 type
// field3 type
// ...
{
To define a struct in Go, you use the type
keyword followed by the struct’s name (alongside the struct
keyword), then you provide a list of its fields enclosed in curly braces. Each field is declared with a name and its respective type.
type Person struct {
name string
age int
email string
}
In the above example, we define a Person
struct with name
of type string
, age
of type int
, and email
of type string
.
Instantiating structs
Once you have defined a struct, you can also create instances (or objects) of that struct. To instantiate a struct, you declare a variable of the struct type, then initialize its fields using the dot notation:
person := Person{
name: "Andy Brinker",
age: 40,
email: "andy@example.com",
}
In the code snippet above, we create a person
instance of the Person
struct, initializing its fields with the provided values.
Now that we’ve seen how to create and instantiate a struct, how do we go upon accessing its values?
Accessing struct values
To access the values belonging to an instance of a struct, you simply use dot notation followed by the field name. Consider the following the example:
fmt.Println(person.name)
fmt.Println(person.age)
fmt.Println(person.email)
As a result of the above operations, we see the following output:
Andy Brinker
40
andy@example.com
At this point, we’ve only seen how we can define and access properties of structs. That is, we’ve really just worked with the data associated with the blueprints we’ve created, such as the name
, age
, and email
of Person
struct. Now the question is: how do we go about using structs to represent not just the data properties of a struct, but also its behavior?
Structs and methods
Go allows you to define methods on structs, which enables you to associate behavior with the data. A method is a function associated with a specific struct type. You can define methods by specifying the receiver type, which determines on which type the method is defined. Consider the following example:
type Rectangle struct {
width float64
height float64
}
// Method defined on the Rectangle struct
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
In the code snippet above, we define a Rectangle
struct with two basic fields: width
and height
. We then define a method called Area()
on the Rectangle
struct, which calculates and returns the area of the rectangle. This allows instances of the Rectangle
struct to not only have data associated with it (e.g., its width
and height
), but also behaviors (i.e., actions) that can be performed by those instances. As such, we can use the Area()
method:
rect := Rectangle{width: 8, height: 6}
area := rect.Area()
fmt.Println(area)
After printing area, we see the following output:
48
Learn more about structs
In Go, a struct is a versatile collection of data fields, each with an associated type. Structs help serve as a “template” or “blueprint” to represent objects, and can be leveraged to create “custom” data types in Go. Borrowing many similar features from object-oriented programming, structs ultimately promote modularity and composition in the programs we write.
For more information about functions and how to leverage them in the Go programs you write, enroll in Udacity’s Golang course.