How to do a literal *int64 in Go?

Go: Literal *int64

In Go, you can create a literal *int64 value using the & operator. The & operator is used to create a pointer to a variable.

Example

package main

import "fmt"

func main() {
    var n int64 = 42
    ptr := &n
    fmt.Println("n:", n)
    fmt.Println("ptr:", ptr)
    fmt.Println("*ptr:", *ptr)

    // Create a literal *int64 value
    litPtr := &int64(42)
    fmt.Println("litPtr:", litPtr)
    fmt.Println("*litPtr:", *litPtr)
}

Explaination

  • First define a variable n of type int64 and initialize it to the value 42.
  • We then use the & operator to create a pointer ptr to the variable n.
  • To create a literal *int64 value, we use the same & operator with the type name int64. This creates a pointer to a new, uninitialized int64 variable, and returns the pointer value. We then assign the pointer value to the variable litPtr.
  • We can then use the * operator to dereference both pointers and print their values.

I hope this example helps you create a literal *int64 value in Go!