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 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),
}
} }