In this article, we explore two essential operators for working with pointers in Go: the Address Operator and the Dereference Operator. A pointer in Go holds the memory address of a variable. To obtain this memory address, you use the Address Operator, represented by the ampersand (&). Conversely, the Dereference Operator, denoted by an asterisk (*), accesses the value stored at that memory address. Consider the following example. Suppose we have a variable x assigned the value 77. The variable x is stored at a specific memory location. When you apply the Address Operator (i.e., &x), it returns the memory address of x (for example, 0x0301). Placing the Dereference Operator in front of the memory address (*0x0301) retrieves the value stored at that location, which is 77.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
In this example, the memory address (e.g., 0x0301) is illustrative. Actual memory addresses will vary when you run the program.
A Practical Example in a Go Program
Let’s see how these operators are used in a simple Go program. In this example, we define the main function, create a variable i with the value 10, and then print both the type and value of its address. We further demonstrate dereferencing the address of i to print its original value.Notice the distinction between the asterisk used in the Dereference Operator and the asterisk seen in the type output (*int). They serve different purposes in the code and the output.