Documentation ¶
Overview ¶
Package pointer contains helper routines for simplifying the creation of optional fields of basic type.
Index ¶
- func Any(v interface{}) interface{}
- func Bool(v bool) *bool
- func Byte(v byte) *byte
- func Complex128(v complex128) *complex128
- func Complex64(v complex64) *complex64
- func Duration(v time.Duration) *time.Duration
- func Float32(v float32) *float32
- func Float64(v float64) *float64
- func Int(v int) *int
- func Int16(v int16) *int16
- func Int32(v int32) *int32
- func Int64(v int64) *int64
- func Int8(v int8) *int8
- func Of[Value any](v Value) *Value
- func Rune(v rune) *rune
- func String(v string) *string
- func Time(v time.Time) *time.Time
- func Uint(v uint) *uint
- func Uint16(v uint16) *uint16
- func Uint32(v uint32) *uint32
- func Uint64(v uint64) *uint64
- func Uint8(v uint8) *uint8
- func Uintptr(v uintptr) *uintptr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Any ¶
func Any(v interface{}) interface{}
Any is a helper routine that allocates a new interface value to store v and returns a pointer to it.
// Usage: var _ *Type = pointer.Any(Type(value) | value).(*Type) var _ *bool = pointer.Any(true).(*bool) var _ *byte = pointer.Any(byte(1)).(*byte) var _ *complex64 = pointer.Any(complex64(1.1)).(*complex64) var _ *complex128 = pointer.Any(complex128(1.1)).(*complex128) var _ *float32 = pointer.Any(float32(1.1)).(*float32) var _ *float64 = pointer.Any(float64(1.1)).(*float64) var _ *int = pointer.Any(int(1)).(*int) var _ *int8 = pointer.Any(int8(8)).(*int8) var _ *int16 = pointer.Any(int16(16)).(*int16) var _ *int32 = pointer.Any(int32(32)).(*int32) var _ *int64 = pointer.Any(int64(64)).(*int64) var _ *rune = pointer.Any(rune(1)).(*rune) var _ *string = pointer.Any("ptr").(*string) var _ *uint = pointer.Any(uint(1)).(*uint) var _ *uint8 = pointer.Any(uint8(8)).(*uint8) var _ *uint16 = pointer.Any(uint16(16)).(*uint16) var _ *uint32 = pointer.Any(uint32(32)).(*uint32) var _ *uint64 = pointer.Any(uint64(64)).(*uint64) var _ *uintptr = pointer.Any(uintptr(64)).(*uintptr)
Example ¶
package main import ( "encoding/json" "fmt" "os" "github.com/xorcare/pointer" ) func main() { type example struct { Bool bool BoolPtr *bool Uint uint UintPtr *uint String string StringPtr *string } encoder := json.NewEncoder(os.Stdout) fmt.Print("Example use universal function: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: pointer.Any(true).(*bool), Uint: 32, UintPtr: pointer.Any(uint(64)).(*uint), String: "xor", StringPtr: pointer.Any("care").(*string), }) fmt.Print("Example use specific functions: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: pointer.Bool(true), Uint: 32, UintPtr: pointer.Uint(64), String: "xor", StringPtr: pointer.String("care"), }) fmt.Print("Example of using a non-standard hack: ") _ = encoder.Encode(example{ Bool: true, BoolPtr: &[]bool{true}[0], Uint: 32, UintPtr: &[]uint{64}[0], String: "xor", StringPtr: &[]string{"care"}[0], }) }
Output: Example use universal function: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"} Example use specific functions: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"} Example of using a non-standard hack: {"Bool":true,"BoolPtr":true,"Uint":32,"UintPtr":64,"String":"xor","StringPtr":"care"}
func Bool ¶
Bool is a helper routine that allocates a new bool value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *bool }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Bool(true) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]bool{true}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *bool <nil> The value set by the library: *bool true The value set in elegant way: *bool true
func Byte ¶
Byte is a helper routine that allocates a new byte value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *byte }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Byte(127) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]byte{255}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint8 <nil> The value set by the library: *uint8 127 The value set in elegant way: *uint8 255
func Complex128 ¶
func Complex128(v complex128) *complex128
Complex128 is a helper routine that allocates a new complex128 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *complex128 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Complex128(complex128(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]complex128{complex128(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *complex128 <nil> The value set by the library: *complex128 (55.753184+0i) The value set in elegant way: *complex128 (48.743702+0i)
func Complex64 ¶
Complex64 is a helper routine that allocates a new complex64 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *complex64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Complex64(complex64(55.751878)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]complex64{complex64(48.752467)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *complex64 <nil> The value set by the library: *complex64 (55.751877+0i) The value set in elegant way: *complex64 (48.75247+0i)
func Duration ¶ added in v1.1.0
Duration is a helper routine that allocates a new time.Duration value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "time" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *time.Duration }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Duration(time.Hour) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]time.Duration{time.Minute}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *time.Duration <nil> The value set by the library: *time.Duration 1h0m0s The value set in elegant way: *time.Duration 1m0s
func Float32 ¶
Float32 is a helper routine that allocates a new float32 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *float32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Float32(float32(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]float32{float32(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *float32 <nil> The value set by the library: *float32 55.753185 The value set in elegant way: *float32 48.743702
func Float64 ¶
Float64 is a helper routine that allocates a new float64 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *float64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Float64(float64(55.753184)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]float64{float64(48.743702)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *float64 <nil> The value set by the library: *float64 55.753184 The value set in elegant way: *float64 48.743702
func Int ¶
Int is a helper routine that allocates a new int value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *int }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int(1) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int{2}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int <nil> The value set by the library: *int 1 The value set in elegant way: *int 2
func Int16 ¶
Int16 is a helper routine that allocates a new int16 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *int16 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int16(16383) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int16{32767}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int16 <nil> The value set by the library: *int16 16383 The value set in elegant way: *int16 32767
func Int32 ¶
Int32 is a helper routine that allocates a new int32 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *int32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int32(1073741823) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int32{2147483647}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int32 <nil> The value set by the library: *int32 1073741823 The value set in elegant way: *int32 2147483647
func Int64 ¶
Int64 is a helper routine that allocates a new int64 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *int64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int64(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int64{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int64 <nil> The value set by the library: *int64 4611686018427387903 The value set in elegant way: *int64 9223372036854775807
func Int8 ¶
Int8 is a helper routine that allocates a new int8 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *int8 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Int8(63) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]int8{127}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int8 <nil> The value set by the library: *int8 63 The value set in elegant way: *int8 127
func Of ¶ added in v1.2.0
func Of[Value any](v Value) *Value
Of is a helper routine that allocates a new any value to store v and returns a pointer to it.
Example ¶
package main import ( "github.com/xorcare/pointer" ) func main() { // Examples for values that have default type casting. var _ *int = pointer.Of(0) var _ *bool = pointer.Of(true) var _ *string = pointer.Of("example") // Example of usage with non default type casting. var _ *int8 = pointer.Of(int8(0)) var _ *int8 = pointer.Of[int8](0) }
Output:
func Rune ¶
Rune is a helper routine that allocates a new rune value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *rune }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Rune(120) fmt.Println(fmt.Sprintf("The value set by the library: %T %3[2]d %[2]q", s.Pointer, *s.Pointer)) s.Pointer = &[]rune{99}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %3[2]d %[2]q", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *int32 <nil> The value set by the library: *int32 120 'x' The value set in elegant way: *int32 99 'c'
func String ¶
String is a helper routine that allocates a new string value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *string }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.String("xor") fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]string{"care"}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *string <nil> The value set by the library: *string xor The value set in elegant way: *string care
func Time ¶ added in v1.1.0
Time is a helper routine that allocates a new time.Time value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "time" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *time.Time }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Time(time.Time{}.Add(time.Minute)) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]time.Time{time.Time{}.Add(time.Hour)}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *time.Time <nil> The value set by the library: *time.Time 0001-01-01 00:01:00 +0000 UTC The value set in elegant way: *time.Time 0001-01-01 01:00:00 +0000 UTC
func Uint ¶
Uint is a helper routine that allocates a new uint value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uint }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint(1) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint{2}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint <nil> The value set by the library: *uint 1 The value set in elegant way: *uint 2
func Uint16 ¶
Uint16 is a helper routine that allocates a new uint16 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uint16 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint16(16383) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint16{32767}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint16 <nil> The value set by the library: *uint16 16383 The value set in elegant way: *uint16 32767
func Uint32 ¶
Uint32 is a helper routine that allocates a new uint32 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uint32 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint32(1073741823) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint32{2147483647}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint32 <nil> The value set by the library: *uint32 1073741823 The value set in elegant way: *uint32 2147483647
func Uint64 ¶
Uint64 is a helper routine that allocates a new uint64 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uint64 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint64(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint64{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint64 <nil> The value set by the library: *uint64 4611686018427387903 The value set in elegant way: *uint64 9223372036854775807
func Uint8 ¶
Uint8 is a helper routine that allocates a new uint8 value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uint8 }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uint8(63) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uint8{127}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uint8 <nil> The value set by the library: *uint8 63 The value set in elegant way: *uint8 127
func Uintptr ¶
Uintptr is a helper routine that allocates a new uintptr value to store v and returns a pointer to it.
Example ¶
package main import ( "fmt" "github.com/xorcare/pointer" ) func main() { s := struct{ Pointer *uintptr }{} fmt.Println(fmt.Sprintf("Zero pointer value: %T %v", s.Pointer, s.Pointer)) s.Pointer = pointer.Uintptr(4611686018427387903) fmt.Println(fmt.Sprintf("The value set by the library: %T %v", s.Pointer, *s.Pointer)) s.Pointer = &[]uintptr{9223372036854775807}[0] fmt.Println(fmt.Sprintf("The value set in elegant way: %T %v", s.Pointer, *s.Pointer)) }
Output: Zero pointer value: *uintptr <nil> The value set by the library: *uintptr 4611686018427387903 The value set in elegant way: *uintptr 9223372036854775807
Types ¶
This section is empty.