1
0
Fork 0

📝 More useful example for pointer package

This commit is contained in:
Maxim Lebedev 2024-03-19 16:09:25 +05:00
parent c867d3a695
commit d9fe14d265
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 28 additions and 12 deletions

View File

@ -1,22 +1,38 @@
package pointer_test
import (
"fmt"
"source.toby3d.me/toby3d/hacks/pointer"
)
func Example() {
val := struct {
Text string
}{Text: "Hello, World!"}
fmt.Printf("Value: %v\n", val)
// NOTE(toby3d): ResponsePayload can hold data in three states:
//
// * nil value
// * 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)
point.Text = "Hello, Go!"
fmt.Printf("Pointer: %v\n", point)
// NOTE(toby3d): the old way
ok := true
text := "hello, world"
num := 0
_ = ResponsePayload{
Text: &text,
Ok: &ok,
Number: &num,
}
// Output:
// Value: {Hello, World!}
// Pointer: &{Hello, Go!}
// NOTE(toby3d): hack-way
_ = ResponsePayload{
Text: pointer.Of("hello, world"),
Ok: pointer.Of(true),
Number: pointer.Of(0),
}
}