To create a pointer to a string in Go, you first need to declare a string variable and then take a reference to it using the &
operator.
Here's an example:
main.go300 chars14 lines
In this example, we create a variable str
of type string
and assign it the value "Hello, Gophers!"
. We then create a pointer variable ptrToStr
of type *string
and initialize it with the address of str
using the &
operator.
We can then print the value of str
, the address of str
, the value of ptrToStr
, and the value that ptrToStr
points to using the *
operator.
Output:
main.go132 chars5 lines
gistlibby LogSnag