Documentation
¶
Overview ¶
Package pointer provides generic utility functions for creating pointers to values and dereferencing pointer values and slices.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dereference ¶
func Dereference[T any](x *T) T
Dereference returns the value of a pointer.
Example ¶
package main
import (
"fmt"
"github.com/verygoodsoftwarenotvirus/platform/v2/pointer"
)
func main() {
s := "world"
val := pointer.Dereference(&s)
fmt.Println(val)
}
Output: world
Example (Nil) ¶
package main
import (
"fmt"
"github.com/verygoodsoftwarenotvirus/platform/v2/pointer"
)
func main() {
val := pointer.Dereference[string](nil)
fmt.Println(val)
}
func DereferenceSlice ¶
func DereferenceSlice[T any](x []*T) []T
DereferenceSlice returns the value of a pointer for every element in a slice.
Example ¶
package main
import (
"fmt"
"github.com/verygoodsoftwarenotvirus/platform/v2/pointer"
)
func main() {
a, b, c := 10, 20, 30
vals := pointer.DereferenceSlice([]*int{&a, &b, &c})
fmt.Println(vals)
}
Output: [10 20 30]
func To ¶
func To[T any](x T) *T
To returns a pointer to a value.
Example ¶
package main
import (
"fmt"
"github.com/verygoodsoftwarenotvirus/platform/v2/pointer"
)
func main() {
p := pointer.To("hello")
fmt.Println(*p)
}
Output: hello
func ToSlice ¶
func ToSlice[T any](x []T) []*T
ToSlice returns the value of a pointer for every element in a slice.
Example ¶
package main
import (
"fmt"
"github.com/verygoodsoftwarenotvirus/platform/v2/pointer"
)
func main() {
vals := []int{1, 2, 3}
ptrs := pointer.ToSlice(vals)
fmt.Println(*ptrs[0], *ptrs[1], *ptrs[2])
}
Output: 1 2 3
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.