Structures in Golang

Structs represent a type of data that is defined by the developer and is used to represent objects. Structures contain a set of fields, that represent the various attributes of the object. The type and struct keywords are used to define the structure:

type structure_name struct{

Each field has a name and data type as a variable. For example, let's define a structure that represents a book:

type book struct{
    title string
    pageNumber int

The structure is called book. It has two fields: name (the book's name, representing the string type) and pageNumber (the number of the pages, representing the int type).

Creating and Initializing a Structure

The structure represents a new type of data, and we can define a variable of that type:

var Golang book

With the help of the initializer, you can pass the initial values to the structure:

var Golang book = book{"Go for begginers", 300}

The initializer represents a set of values in curly braces. Moreover, these values are passed to the fields of the structure in the order in which where the fields are defined in the structure. For example, in this case, the string "Golang" is passed to the first field, title, and the second value "300" is passed to the second field, pageNumber.

We can also explicitly specify which values are passed to the properties:

var PostgreSQL book = book{pageNumber: 400, title: "Learn PostgreSQL with Examples"}

You can also use abbreviated ways to initialize a structure variable:

var PostgreSQL = book {title: "Learn PostgreSQL", pageNumber: 400}
Rust := book {title: "Learn Rust", pageNumber: 312}

You don't even have to specify any values, in which case the structure properties will get the default values:

undefined := book {} 

 Access Fields of a Struct

To refer to the fields of the structure, put a period after the variable and specify the field of the structure:

package main
import "fmt"
type book struct{
    title string
    pageNumber int
func main() {     
    var postgres = book {title: "Learn PostgreSQL", pageNumber: 300}
    postgres.pageNumber = 400    
    fmt.Println(postgres.title, postgres.pageNumber)   

And if we run the code we will get:

Learn PostgreSQL
Learn PostgreSQL 400

Pointers to a Struct

As with regular variables, you can create pointers to structures.

package main

import "fmt"

type book struct {
    title      string
    pageNumber int

func main() {

    var postgres = book{title: "Learn PostgreSQL", pageNumber: 300}
    var pp *book = &postgres

    fmt.Println("the page number before", postgres.pageNumber)
    pp.pageNumber = 500

    fmt.Println("the page number after ", pp.pageNumber)


The output of this will be:

the page number before 300
the page number after  500

It is not necessary to assign a variable address to a pointer to a structure to initialize it. You can assign the address of an unnamed object as follows:

var Rust *book = &book{title:"learn Rust", pageNumber: 300}
var Golang *book = new(book)

To access the fields of the structure through a pointer, you can use the dereference operation (), or you can access the pointer directly ():