Questions And Answers

More Tutorials

Go Pointers

Go support pointers. A pointer holds the memory address of a value. It allows you to pass references to values and records within your program.

The type *T is a pointer to a T value. Its zero value is nil.

var p *int

The & operator generates a pointer to its operand.

i := 42
p = &i

The * operator denotes the pointer's underlying value.

fmt.Println(*p) // read i through the pointer p
*p = 21         // set i through the pointer p

How to declare and access pointer variable?

package main

import "fmt"

func main() {
   var actualVariableFirstName string = "Jhon"   /* variable declaration */
   var pointerVariable *string      /* pointer variable declaration */

/* store address of actual variable in pointer variable*/	
   pointerVariable = &actualVariableFirstName

   fmt.Printf("\nAddress of variable: %v", &actualVariableFirstName)
   fmt.Printf("\nAddress stored in pointer variable: %v", pointerVariable)
   fmt.Printf("\nValue of Actual Variable: %s",actualVariableFirstName)
   fmt.Printf("\nValue of Pointer variable: %s",*pointerVariable)


Address of variable: 0xc000010200
Address stored in pointer variable: 0xc000010200
Value of Actual Variable: Jhon
Value of Pointer variable: Jhon

Go Pointers another example

package main

import "fmt"

func zeroval(ival int) {
    ival = 0

func zeroptr(iptr *int) {
    *iptr = 0

func main() {
    i := 1
    fmt.Println("initial:", i)

    fmt.Println("zeroval:", i)

    fmt.Println("zeroptr:", i)

    fmt.Println("pointer:", &i)


initial: 1
zeroval: 1
zeroptr: 0
pointer: 0xc000014050


In this page (written and validated by ) you learned about Go Pointers . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go HTTP Request using Get.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.