types

package
v1.2.117 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any added in v1.2.91

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

Any returns its argument v or nil if and only if v is a typed nil or an untyped nil. For example, if v was created by set with `var p *int` or calling Any((*int)(nil)), Any returns nil.

Example
package main

import (
	"fmt"

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

type stringerVal struct {
	s string
}

func (s stringerVal) String() string {
	return s.s
}

func main() {

	fmt.Printf("%#v\n", (*stringerVal)(nil)) // typed nil implements [fmt.Stringer]
	fmt.Printf("%#v\n", types.Any((*stringerVal)(nil)))

}
Output:

(*types_test.stringerVal)(nil)
<nil>

func CompareOptional added in v1.2.77

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

func Conv added in v1.2.100

func Conv[T, S constraints.Number](v T) S

Conv converts a number[T] into an unlimited untyped number, and assigned back to number[S]. static_cast<S>(v) https://go.dev/ref/spec#Constants

Example
package main

import (
	"fmt"
	"math"

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

func main() {

	fmt.Printf("%#x\n", types.Conv[int8, uint8](-1))           // overflow
	fmt.Printf("%d\n", types.Conv[uint8, int8](math.MaxUint8)) // overflow
	fmt.Printf("%d\n", types.Conv[float32, int32](-1))
	fmt.Printf("%f\n", types.Conv[int32, float32](-1))
}
Output:

0xff
-1
-1
-1.000000

func ConvBinary added in v1.2.100

func ConvBinary[T, S any](v T) S

ConvBinary writes a number[T] into a byte slice, and reads the byte slice into a number[S]. binary.Write( T -> []byte ) => binary.Read( []byte, S )

Example
fmt.Printf("%#x\n", types.ConvBinary[int8, uint8](-1))           // overflow
fmt.Printf("%d\n", types.ConvBinary[uint8, int8](math.MaxUint8)) // overflow
fmt.Printf("%d\n", types.ConvBinary[float32, int32](-1))
fmt.Printf("%f\n", types.ConvBinary[int32, float32](-1082130432))
fmt.Printf("%d\n", types.ConvBinary[*stringerVal, int64]((*stringerVal)(nil)))
fmt.Printf("%d\n", types.ConvBinary[*stringerVal, int64](nil))
Output:

0xff
-1
-1082130432
-1.000000
0
0

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