How to Use Structs in Golang

In this blog post, we discuss how to use structs in Golang. After the end of the lesson, you’ll have learnt how to implement structs in your Golang programs.

Structs are typed collections that are used to stored fields. They are useful when you want to group your data to form the records. Why are structs important to a Golang programmer?

Now, you’ve learnt how to write Golang programs using various built-in data types. While using data types is crucial, there comes a time when you need to use structs instead of built-in data types, especially when you have so many data types that can be grouped together. Using so many data types and variables is tedious.

Consider the Golang program below:

package main
import ("fmt"; "math")
func coordinates(a, b, c, d float32) float32{
m := b – a
n := d-c
return math.Sqrt (m*m - n*n)
}

It’s difficult to keep track of all coordinates and see how the program is running especially if you have so many variables to work with. So, how can structs simplify such a program?

First, structs are specified using type—that groups related fields into form records—and the “struct” keywords. Here’s the syntax of using structs in Golang:

type name_of_the_struct  struct { }

In this case, the name_of_the_struct is an identifier—that must adhere to naming convention rules. The example below illustrates how you can create a struct in Golang:

type myshape struct { }

In the above case, the name of the struct is “myshape”. What’s placed in “{ }” are the implementations of the struct.

Now, just like classes in object oriented programming, structs can contain some other data—that means you’ve to declare the various data types that you wish to have in your struct. That means that you’ll be representing contents of any real world object into the computer’s memory. Here’s an example:

type shape1 struct {
a int32 
b int32
c string
}
type shape2 struct {
d, e int 
g float32
}

Next up, how can you use structs in your Golang code? Keep reading.

Initialization of structs
You can create an instance of the “shape1” in so many ways. First, you can use the syntax below to create an instance of “shape1”:

var h shape1

The above initialization creates a local shape1 variable that will be set to zero, by default. This means the values for variables a and b will all be set to 0 since they are integers. However, the variable c will be set to “nil” since it’s a string.
Secondly, you can initialize an instance of “shape1” using the syntax below:

i := new(shape1)

The above initialization allocates memory for all the fields—a, b and c—and sets each of fields to 0 value and returns a pointer.
Thirdly, if you want to set different values for your fields instead of 0, you can use the syntax below:

i := shape1{a: 5, b: 16, c: “This an example of structs in Golang”}

Finally, you can use the syntax below to initialize your structs:

i := shape1{5, 16, “This an example of structs in Golang”}

What about displaying fields in Golang?
Well, to display the field in Golang, we use the “.” operator. Here’s an example of how to display fields while using structs in Golang:

fmt.Println (i.a, i.b, i.c)

Finally, let’s have an example that illustrates Golang’s struct:

package main
import "fmt"
type Rect struct {
l, w int 
}
func main() {
    r := Rect{}  
    fmt.Println("The default values of the rectangle are: ", r) 
}

Now you know how to use structs in Golang programs. It’s important to adhere to the syntactic rules when using structs to have efficient Golang programs.

GET YOUR FREE Golang EBOOK!

(Visited 1,790 times, 1 visits today)

Comments