create a pointer to an array in go

In Go, you can create a pointer to an array by using the ampersand (&) operator followed by the name of the array. Here's an example:

main.go
package main

import "fmt"

func main() {
  // create an array
  arr := [3]int{1, 2, 3}

  // create a pointer to the array
  ptr := &arr

  // print the pointer's value
  fmt.Println(ptr)
}
191 chars
15 lines

In the above example, we create an array of three integers and then create a pointer to it using the ampersand operator. We then print the pointer's value to the console, which will display the memory address of the array.

related categories

gistlibby LogSnag