Structures

Although arrays, slices, and maps are all very useful, they cannot group and hold multiple values in the same place. When you need to group various types of variables and create a new handy type, you can use a structure. The various elements of a structure are called fields of the structure or just fields.

We will start this section by explaining a simple structure that was first defined in the sortSlice.go source file of the previous chapter:

type aStructure struct { 
    person string 
    height int 
    weight int 
} 

For reasons that will become evident in Chapter 6, What You Might Not Know About Go Packages, the fields of a structure usually begin with an uppercase letter. This convention will be used throughout the rest of the book.

This structure has three fields named person, height, and weight, respectively. You can create a new variable of the aStructure type as follows:

var s1 aStructure 

Additionally, you can access a specific field of a structure by its name. So, in order to get the value of the person field of the s1 variable, you should type s1.person.

A structure literal can be defined as follows:

p1 := aStructure{"fmt", 12, -2}

However, since remembering the order of the fields of a structure can be pretty hard, Go allows you to use another form for defining a structure literal:

p1 := aStructure{weight: 12, height: -2} 

In this case, you do not need to define an initial value for every field of the structure.

Now that you know the basics of structures, it is time to show you a more practical example, which is named structures.go and will be presented in four parts.

The first part of structures.go contains the following code:

package main 
 
import ( 
    "fmt" 
)

Structures in particular and Go types in general are usually defined outside the main() function in order to have a global scope and be available to the entire Go package, unless you want to clarify that a type is only useful within the current scope and is not expected to be used elsewhere.

The second code segment of structures.go is shown in the following Go code:

func main() { 
 
    type XYZ struct { 
        X int 
        Y int 
        Z int 
    } 
 
    var s1 XYZ 
    fmt.Println(s1.Y, s1.Z) 

As you can see, there is nothing that prevents you from defining a new structure type inside a function, but you should have a reason for doing so.

The third portion of structures.go follows next:

    p1 := XYZ{23, 12, -2} 
    p2 := XYZ{Z: 12, Y: 13} 
    fmt.Println(p1) 
    fmt.Println(p2) 

Here you define two structure literals named p1 and p2 that you print afterwards.

The last part of structures.go contains the following Go code:

    pSlice := [4]XYZ{} 
    pSlice[2] = p1 
    pSlice[0] = p2 
    fmt.Println(pSlice) 
    p2 = XYZ{1, 2, 3} 
    fmt.Println(pSlice) 
} 

In this last part, we created an array of structures named pSlice. As you will understand from the output of structures.go, when you assign a structure to an array of structures, the structure is copied into the array, so changing the value of the original structure will have no effect on the objects of the array.

Executing structures.go will generate the following output:

$ go run structures.go
0 0
{23 12 -2}
{0 13 12}
[{0 13 12} {0 0 0} {23 12 -2} {0 0 0}]
[{0 13 12} {0 0 0} {23 12 -2} {0 0 0}]

Note that the order in which you put the fields in the definition of a structure type is significant for the type identity of the defined structure. Put simply, two structures with the same fields will not be considered identical in Go if their fields are not in exactly the same order.

The output of structures.go illustrates that the zero value of a struct variable is constructed by zeroing all of the fields of the struct variable according to their types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.23.147