Pointers to structures

In the previous chapter, we talked about pointers. In this section, we will continue our discussion of pointers and present an example that is related to pointers to structures. The name of the program is pointerStruct.go and will be presented in four parts.

The first part of the program contains the following Go code:

package main 
 
import ( 
    "fmt" 
) 
 
type myStructure struct { 
    Name    string 
    Surname string 
    Height  int32 
} 

The second code segment from pointerStruct.go follows next:

func createStruct(n, s string, h int32) *myStructure { 
    if h > 300 { 
        h = 0 
    } 
    return &myStructure{n, s, h} 
} 

The approach used in createStruct() for creating a new structure variable has many advantages over initializing structure variables on your own, including the fact that you are allowed to check whether the information provided is both correct and valid. Additionally, this approach is cleaner-there is a central point where structure variables are initialized so that if there is something wrong with your struct variables, you know where to look and who to blame! Note that some people might prefer to name the createStruct() function as NewStruct().

For those with a C or C++ background, it is perfectly legal for a Go function to return the memory address of a local variable. Nothing gets lost, so everybody is happy!

The third portion of pointerStruct.go is as follows:

func retStructure(n, s string, h int32) myStructure { 
    if h > 300 { 
        h = 0 
    } 
    return myStructure{n, s, h} 
} 

This part presents the no pointer version of the createStruct() function named retStructure(). Both functions work fine, so choosing between the implementation of createStruct() and retStructure() is just a matter of personal preference. A more appropriate name for these functions might have been NewStructurePointer and NewStructure, respectively.

The last part of pointerStruct.go is shown in the following Go code:

func main() { 
    s1 := createStruct("Mihalis", "Tsoukalos", 123) 
    s2 := retStructure("Mihalis", "Tsoukalos", 123) 
    fmt.Println((*s1).Name) 
    fmt.Println(s2.Name) 
    fmt.Println(s1) 
    fmt.Println(s2) 
} 

If you execute pointerStruct.go, you will get the following output:

$ go run pointerStruct.go
Mihalis
Mihalis
&{Mihalis Tsoukalos 123}
{Mihalis Tsoukalos 123}

Here you can see one more time that the main difference between createStruct() and retStructure() is that the former returns a pointer to a structure, which means that you will need to dereference that pointer in order to use the object to which it points. This can make your code look a little uglier.

Structures are very important in Go, and they are being used extensively in real-world programs because they allow you to group as many values as you want and treat those values as a single entity.

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

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