DEV Community

Cover image for What are pointers in golang
Abdulrahman Masoud
Abdulrahman Masoud

Posted on

What are pointers in golang

Hi, I'm Abdulrahman, and I'm glad to share this blog, in this blog we will talk about Pointers in golang.

Pointer is a variable that references a memory location where a value is stored so before digging deeper into pointers we should know what are variables and how variables work.

What are variables

Variables are placeholder of the information to store the value for any data type you need like string, integr, boolen, float, etc..
basically, if we have a string we can assign it to any variable, and then we can access this string by the name of the variable declared, so how variable stored this string
every variable you assign it, so when you assign a variable it will be assigned to a memory address in the computer.

Let's deg deeper into pointers

Pointers is a way to store the memory address for any type of variable.

Why we need to use pointers?

first we need to know how variables works like we explain above is the name given to a memory location is pointing to the stored data
we need pointers to store the memory address of another variable to can access the value of this memory location to make any operation on this value you can change the value for this address, note: when you try to change this value by memory address you actually replace the old value by the new value.

Syntax to declare a pointers

first, you need to know the two important operators we will use in pointers

  • *: this asterisk operator used to declare pointer variable
  • &: this and to return the address of a variable you are added a pointer on it

so to declare a pointer, we need to use * and assign this asterisk to the data type in the variable declared, and you need to use & before the new variable to get the memory address for the pointed variable

Examples

Example 1
package main

import "fmt"

func main() {
    var name *string
    me := &name
    fmt.Println(name) // Result nil
    fmt.Println(me)   // Result 0xc00000e028
}
Enter fullscreen mode Exit fullscreen mode

in the above example, we declare a new variable name and make it a pointer by adding * to the data type and declare another variable
me and make it pointed to a memory address for variable name by adding & to the name variable, so let's see the results
result for variable name is nil why nil? because we didn't assign any value for it
and the result for variable me is 0xc00000e028 what is this hash? this is the address for a location in memory, why return this result? because we declare variable me as a reference for variable name and this reference pointed to the memory address for name variable in the memory location.

Example 2
package main

import "fmt"

func main() {
    var x int = 100
    var y *int = &x
    *y = 50
    fmt.Println(x) //Result 50
    fmt.Println(y) //Result  0xc0000ba000
}
Enter fullscreen mode Exit fullscreen mode

in the above example, we declare a variable x and assign 100 value to it, and declare another variable y it's a pointer and its value of it is a reference to variable x
and assign a 50 for a pointer of y variable,
so why the result of x is 50? because we add the reference of variable x to pointer y here y is pointed a memory address of x if we change the value of y or x it will change both because these two variables pointed to the same memory address
if we assigned a new value for x it will change the value for *y like this example

package main
import "fmt"
func main() {
    var x int = 100
    var y *int = &x
    *y = 50
    x = 25
    fmt.Println(*y) //Result 25
    fmt.Println(y) //Result  0xc0000ba000
}
Enter fullscreen mode Exit fullscreen mode

here result will be 25 why? because the variables has the same memory address.

Note: when we need to print a value for a pointer we need to print it using * like we did above with variable y *y and if we tried to print it without add * before it will print a memory address for this variable

Example 2 (Pointer to a Struct Syntax)

pointer to a struct is just like any other pointer.

package main

import "fmt"

type User struct {
    FirstName string
    LastName  string
}

func main() {
    var user *User
    abdulrahman := User{"Abdulrahman", "Masoud"}
    user = &abdulrahman

    fmt.Println(&user)          //0xc00000e028
    fmt.Println(user.FirstName) //Abdulrahman
}
Enter fullscreen mode Exit fullscreen mode

in the above example we declare a struct User it has FirstName and LastName and in main function and add a new var user to assign a pointer of struct User
and add a new user called abdulrahman to make a new user and tell user var is equal a reference of abdulrahman

Example 3 (Pointer to a Struct)

We can simply pass a pointer to a struct in the function and then manipulate the struct using the function via the pointer. Let’s see how one would do that.

package main

import "fmt"

type User struct {
    FirstName string
    LastName  string
}
func change(user *User) {
    user.LastName = "Masoud"
}
func main() {
    user := User{"Abdulrahman", "Salah"}
    fmt.Println(user) //{Abdulrahman Salah}
    change(&user)
    fmt.Println(user) //{Abdulrahman Masoud}
}
Enter fullscreen mode Exit fullscreen mode

in above example we declare a new function change and pass a pointer of User as argument and then use it to change the LastName
What happens is that,the pointer changes it at the original value.

This is extremely useful since we want to change the original rather than a copy of it.

End of blog

Summary

  • Variables are placeholder of the information to store the value for any data type.
  • Pointer is a variable that references a memory location.
  • when you assign a variable it will be assigned to a memory address
  • we need pointers to store the memory address of another variable
  • when you try to change the value by memory address you actually replace the old value by the new value.
  • * asterisk operator used to declare pointer variable
  • & and operator to return the address of a variable you are added a pointer on it
  • pointer to a struct is just like any other pointer.

I hope you enjoy this kickoff to a new blog in Golang,
Looking forward to sharing these blogs with you.

Top comments (0)