types

package
v1.2.77 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareOptional added in v1.2.77

func CompareOptional[E constraints.Ordered](a, b Optional[E]) int

func Pointer

func Pointer[T any](v T) *T

Pointer returns a pointer to the value passed in.

func PointerMap

func PointerMap[M ~map[K]V, N map[K]*V, K comparable, V any](src M) N

PointerMap converts a map of values into a map of pointers

func PointerSlice

func PointerSlice[S ~[]E, V []*E, E any](src S) V

PointerSlice converts a slice of values into a slice of pointers

func Value

func Value[T any](v *T) T

Value returns the value of the pointer passed in or "" if the pointer is nil.

func ValueMap

func ValueMap[M ~map[K]*V, N map[K]V, K comparable, V any](src M) N

ValueMap converts a map of string pointers into a string map of values

func ValueSlice

func ValueSlice[S ~[]*E, V []E, E any](src S) V

ValueSlice converts a slice of pointers into a slice of values

Types

type Optional added in v1.2.77

type Optional[E any] struct {
	Value E
	Valid bool // Valid is true if Value is not NULL
}

Optional represents a Value that may be null.

func NullOpt deprecated added in v1.2.77

func NullOpt[E any]() Optional[E]

NullOpt is the null Optional.

Deprecated: Use a literal types.Optional[E]{} instead.

func Opt added in v1.2.77

func Opt[E any](e E) Optional[E]

func (Optional[E]) Format added in v1.2.77

func (o Optional[E]) Format(s fmt.State, verb rune)
Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/exp/types"
)

func main() {
	fmt.Printf("'%+v'\n", types.Optional[string]{})
	fmt.Printf("'%v'\n", types.Optional[string]{})
	fmt.Printf("'%s'\n", types.Optional[string]{})
	fmt.Printf("'%+v'\n", types.Optional[int]{})
	fmt.Printf("'%v'\n", types.Optional[int]{})
	fmt.Printf("'%s'\n", types.Optional[int]{})
	fmt.Printf("'%+v'\n", types.Optional[*int]{})
	fmt.Printf("'%v'\n", types.Optional[*int]{})
	fmt.Printf("'%s'\n", types.Optional[*int]{})

	fmt.Printf("'%+v'\n", types.Opt(""))
	fmt.Printf("'%v'\n", types.Opt(""))
	fmt.Printf("'%s'\n", types.Opt(""))
	fmt.Printf("'%+v'\n", types.Opt(0))
	fmt.Printf("'%v'\n", types.Opt(0))
	fmt.Printf("'%s'\n", types.Opt(0))
	fmt.Printf("'%+v'\n", types.Opt[*int](nil))
	fmt.Printf("'%v'\n", types.Opt[*int](nil))
	fmt.Printf("'%s'\n", types.Opt[*int](nil))

	fmt.Printf("'%+v'\n", types.Opt("hello world"))
	fmt.Printf("'%v'\n", types.Opt("hello world"))
	fmt.Printf("'%s'\n", types.Opt("hello world"))
	fmt.Printf("'%+v'\n", types.Opt(0xFF))
	fmt.Printf("'%v'\n", types.Opt(0xFF))
	fmt.Printf("'%s'\n", types.Opt(0xFF))
	var p int
	fmt.Printf("'%s'\n", fmt.Sprintf("%+v", types.Opt(&p))[:2])
	fmt.Printf("'%s'\n", fmt.Sprintf("%v", types.Opt(&p))[:2])
	fmt.Printf("'%s'\n", fmt.Sprintf("%s", types.Opt(&p))[:11])

}
Output:

'null string: '
'null'
'null'
'null int: 0'
'null'
'null'
'null *int: <nil>'
'null'
'null'
''
''
''
'0'
'0'
'%!s(int=0)'
'<nil>'
'<nil>'
'%!s(*int=<nil>)'
'hello world'
'hello world'
'hello world'
'255'
'255'
'%!s(int=255)'
'0x'
'0x'
'%!s(*int=0x'

func (Optional[E]) String added in v1.2.77

func (o Optional[E]) String() string
Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/exp/types"
)

func main() {
	fmt.Printf("%s\n", types.Optional[string]{}.String())
	fmt.Printf("%s\n", types.Optional[int]{}.String())
	fmt.Printf("%s\n", types.Optional[*int]{}.String())

	fmt.Printf("%s\n", types.Opt("").String())
	fmt.Printf("%s\n", types.Opt(0).String())
	fmt.Printf("%s\n", types.Opt[*int](nil).String())

	fmt.Printf("%s\n", types.Opt("hello world").String())
	fmt.Printf("%s\n", types.Opt(0xFF).String())
	var p int
	fmt.Printf("%s\n", types.Opt(&p).String()[:2])

}
Output:

null
null
null

0
<nil>
hello world
255
0x

func (Optional[E]) ValueOr added in v1.2.77

func (o Optional[E]) ValueOr(e E) E

ValueOr returns the contained value if available, another value otherwise

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL