Created
January 19, 2023 10:13
-
-
Save lmllrjr/3802598c503a79f44dec4ee73cacec9b to your computer and use it in GitHub Desktop.
Create/use a pointer to a slice of pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check it out in go playground: | |
// https://go.dev/play/p/4FJZ7109K4U | |
package main | |
import "fmt" | |
func main() { | |
p := NewPointers() | |
fmt.Printf("\nthe complete pointer to slice of pointers (c): %#v\n", p) | |
fmt.Printf("\n(a)value of first entry (a): %#v\n", *(*p)[0]) | |
fmt.Printf("\n(b)value of second entry (b): %#v\n", *(*p)[1]) | |
} | |
// NewPointers creates a pointer to a slice of pointers of strings | |
func NewPointers() *[]*string { | |
a := "a" | |
b := "b" | |
c := make([]*string, 2) | |
c[0] = &a | |
c[1] = &b | |
return &c | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment