The Journey of #100DaysofCode (@eliehannouch)

100daysofcode - Day 12

Day 12 is already here :smiling_face_with_three_hearts::tada:. Hope you are doing well and enjoying your journeys to the maximum. So guys, it’s time to continue and dive more into some interesting topics in Golang, to wrap up the day successfully. :fist::white_check_mark:

In today’s post, we will talk about pointers in Go, their role and where to use them. Moving from some theoretical definitions to an interactive funny :star_struck: example that illustrate the targeted topics in a clear way :heart_eyes::partying_face:

What is a pointer in Golang ? :round_pushpin::thinking:

  • A pointer is simply a normal :face_holding_back_tears: variable that stores the address of a certain object stored in the memory.

  • A pointer points to different variables with different data types like int , string , bool and more and only stores their memory address in hexadecimal :smiling_face: format. :sunglasses:

  • In Golang, pointers are called special :astonished: variables.

Why do we use pointers in Go ? :face_with_monocle::thinking:

  • Pointers are used when we need to pass :ticket: a variable by reference to a function or a method. :partying_face:
  • Pointers can be used to point :pushpin: out an array, a struct, an interface … in Golang.
  • Golang pointers avoid :no_entry: unsafe :radioactive: operations like updating or manipulating a value.

Pass by reference :vs: Pass by value :thought_balloon:

  • When we pass by value, a copy of the variable gets passed to the function to be used. This copy will be updated as required, but the original will be untouched. :blush:
  • On the other hand :rightwards_hand: , while passing a variable by reference, this means that we are passing the address :card_index: of the original variable and any operation will affect the original data stored in the variable. :smiling_face_with_three_hearts:

What is a dereference operator in Golang pointer ? :star_struck:

  • ﹡operator: the * is used to declare a :new: pointer in Go, we can use it also to access the data value from the address where the pointer is pointing. :wink:

What is the address :house: operator in Golang ?

  • & operator: the & is used to get the address :card_index: of the variable

How to declare a pointer :round_pushpin: in Golang :blush:?

  • var pointerName *Type
  • pointerName: the name of the pointer name
  • Type: the variable data type to where the pointer is pointing.
  • Example of int and string pointer:
    package main
     
    import "fmt"
     
    func main() {
       // A variable of type int
       var integerNumber int
       integerNumber = 34 // A number of type int
     
       // A pointer of type string
       var stringPointer *string
      
       // A pointer of type int
       var integerPointer *int
     
       // integerPointer pointing to integerNumber
       integerPointer = &integerNumber
     
       fmt.Println("The address on which the pointer is pointing", integerPointer)
       // Output: output in hex is 0x1400001c070
     
       fmt.Println("The value of the variable to which the pointer is pointing", *integerPointer)
       // Output: The value is: 34
     
       // To change the value of the variable via the pointer we use:
     
       *integerPointer = 45
     
       fmt.Println("The new value of the variable integerNumber is: ", integerNumber)
       // Output: 45
    }
 

Create a pointer using the :new: keyword :smiling_face_with_three_hearts:

    package main
     
    import "fmt"
     
    func main() {
     
       // create a pointer using new()
       var newPointer = new(string)
     
       *newPointer = "elie hannouch"
     
       fmt.Println(newPointer)  // 0x14000010230
       fmt.Println(*newPointer) // elie hannouch
     
    }

Functions with pointers :heart_eyes:

    package main
     
    import "fmt"
     
    func updateName(name *string) {
       // Using the dereference operator we change
       // value of the name variable
       *name = "Elie Hannouch"
     
    }
     
    func main() {
       // Name with initial value
       var name string = "John Doe"
       fmt.Println("The original name is", name) // John Doe
     
       // Passing the address of the name variable to the updateName function
       updateName(&name)
       fmt.Println("The original name is now changed to", name) // Elie Hannouch
    }

  • Stay tuned for the next posts , where we will talk about pointers with structs , methods and more :star_struck:
5 Likes