📝 More useful example for pointer package
This commit is contained in:
parent
c867d3a695
commit
d9fe14d265
|
@ -1,22 +1,38 @@
|
||||||
package pointer_test
|
package pointer_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"source.toby3d.me/toby3d/hacks/pointer"
|
"source.toby3d.me/toby3d/hacks/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Example() {
|
func Example() {
|
||||||
val := struct {
|
// NOTE(toby3d): ResponsePayload can hold data in three states:
|
||||||
Text string
|
//
|
||||||
}{Text: "Hello, World!"}
|
// * nil value
|
||||||
fmt.Printf("Value: %v\n", val)
|
// * zero value: "", 0, false
|
||||||
|
// * any valid value: "abc", 42, true
|
||||||
|
//
|
||||||
|
// So, for support all these states struct must have pointers to basic
|
||||||
|
// types.
|
||||||
|
type ResponsePayload struct {
|
||||||
|
Text *string
|
||||||
|
Ok *bool
|
||||||
|
Number *int
|
||||||
|
}
|
||||||
|
|
||||||
point := pointer.Of(val)
|
// NOTE(toby3d): the old way
|
||||||
point.Text = "Hello, Go!"
|
ok := true
|
||||||
fmt.Printf("Pointer: %v\n", point)
|
text := "hello, world"
|
||||||
|
num := 0
|
||||||
|
_ = ResponsePayload{
|
||||||
|
Text: &text,
|
||||||
|
Ok: &ok,
|
||||||
|
Number: &num,
|
||||||
|
}
|
||||||
|
|
||||||
// Output:
|
// NOTE(toby3d): hack-way
|
||||||
// Value: {Hello, World!}
|
_ = ResponsePayload{
|
||||||
// Pointer: &{Hello, Go!}
|
Text: pointer.Of("hello, world"),
|
||||||
|
Ok: pointer.Of(true),
|
||||||
|
Number: pointer.Of(0),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue