-2

*with a note I just learned golang

in my mind I just think why there should be a pointer, if you want to enter the value of variable a to variable b only with this varA := 10, varB:= varA(sry idk how the code to the nextline) and I'm confused when to use a pointer?

ade bayu s
  • 104
  • 9

2 Answers2

4

Please follow golang tour. https://go.dev/tour/moretypes/1

Pointers in Go programming language or Golang is a variable that is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.)[https://www.geeksforgeeks.org/pointers-in-golang]. Making a copy of large data types like struct before passing would take time and would consume memory. This is another reason why programmers prefer pointers for big data types.

An example use of pointers

package main
 
import "fmt"
 
func main() {
 
    // taking a normal variable
    var x int = 5748
     
    // declaration of pointer
    var p *int
     
    // initialization of pointer
    p = &x
 
        // displaying the result
    fmt.Println("Value stored in x = ", x)
    fmt.Println("Address of x = ", &x)
    fmt.Println("Value stored in variable p = ", p)
}

output:

Value stored in x =  5748
Address of x =  0x414020
Value stored in variable p =  0x414020

Check out the following articles which may be useful for you :

pixylife
  • 463
  • 4
  • 12
  • 1
    "Pointers save memory space" I'm not sure that's entirely correct - consider a pointer to a `int8`. See the answers to the question I linked for some fairly detailed answers. – Brits Aug 07 '22 at 07:59
  • sure I'll check that. thanks for the information – pixylife Aug 07 '22 at 08:43
2

When to use a value and pointer depends on what you're going to do with the data, and will affect how it's stored in memory.

If you plan to operate on the data only in your local function, use the value. The stores the data in the context of the local function itself (called the stack) and has almost no garbage collection overhead.

If you plan to pass the data elsewhere to another function, and you do not want the code in that function to affect this function's view of the data, use the value. That way, when you pass the data anywhere else, you'll be passing a copy of the data. This copy will exist on that function's local stack.

If you plan to pass the data elsewhere but want the changes that happen to it there to be visible to your code here, or if you data is too large to copy, use a pointer. When you have a pointer you're actually holding the memory address of the data instead of the data itself, so when you pass this address around all changes made anywhere affect the original data - the data itself is never copied. This is done by moving the data to a kind of global data storage system called the heap, and Go will delete the data off the heap only after it determines that all the places using that address are no longer functioning (the process called garbage collection).

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90