Documentation ¶
Overview ¶
Package gox contains functions and types which could have been builtin, which could have been part of Go itself.
Most of the functions are eligible for inlining.
Reasonable to "dot-import" the package so identifiers will be directly available, see the package example.
Example ¶
// Pointers to non-zero values b, i, s := NewBool(true), NewUint(1), NewString("hi") fmt.Printf("b: %t, i: %d, s: %s\n", *b, *i, *s) // One way of mimicing the ternary operator: for _, age := range []int{10, 20} { state := If(age < 18).String("child", "adult") fmt.Printf("Age: %d, state: %s\n", age, state) } // And another one: for _, tempC := range []int{-5, 10} { fmt.Printf("Temperature: %d°C, state: %s\n", tempC, IfString(tempC < 0, "solid", "liquid")) } // Pass multiple return values to variadic functions: now := time.Date(2020, 3, 4, 0, 0, 0, 0, time.UTC) fmt.Printf("Year: %d, month: %d, day: %d\n", Wrap(now.Date())...) // Quick "handling" of error: n, err := strconv.Atoi("3") Pie(err) fmt.Println("Parsed:", n)
Output: b: true, i: 1, s: hi Age: 10, state: child Age: 20, state: adult Temperature: -5°C, state: solid Temperature: 10°C, state: liquid Year: 2020, month: 3, day: 4 Parsed: 3
Index ¶
- func IfBool(c bool, a, b bool) bool
- func IfByte(c bool, a, b byte) byte
- func IfFloat32(c bool, a, b float32) float32
- func IfFloat64(c bool, a, b float64) float64
- func IfIf(c bool, a, b interface{}) interface{}
- func IfInt(c bool, a, b int) int
- func IfInt16(c bool, a, b int16) int16
- func IfInt32(c bool, a, b int32) int32
- func IfInt64(c bool, a, b int64) int64
- func IfInt8(c bool, a, b int8) int8
- func IfRune(c bool, a, b rune) rune
- func IfString(c bool, a, b string) string
- func IfUint(c bool, a, b uint) uint
- func IfUint16(c bool, a, b uint16) uint16
- func IfUint32(c bool, a, b uint32) uint32
- func IfUint64(c bool, a, b uint64) uint64
- func IfUint8(c bool, a, b uint8) uint8
- func NewBool(b bool) *bool
- func NewByte(b byte) *byte
- func NewFloat32(f float32) *float32
- func NewFloat64(f float64) *float64
- func NewInt(i int) *int
- func NewInt16(i int16) *int16
- func NewInt32(i int32) *int32
- func NewInt64(i int64) *int64
- func NewInt8(i int8) *int8
- func NewRune(r rune) *rune
- func NewString(s string) *string
- func NewUint(i uint) *uint
- func NewUint16(i uint16) *uint16
- func NewUint32(i uint32) *uint32
- func NewUint64(i uint64) *uint64
- func NewUint8(i uint8) *uint8
- func Pie(err error)
- func Wrap(vs ...interface{}) []interface{}
- type If
- func (c If) Bool(a, b bool) bool
- func (c If) Byte(a, b byte) byte
- func (c If) Float32(a, b float32) float32
- func (c If) Float64(a, b float64) float64
- func (c If) If(a, b interface{}) interface{}
- func (c If) Int(a, b int) int
- func (c If) Int16(a, b int16) int16
- func (c If) Int32(a, b int32) int32
- func (c If) Int64(a, b int64) int64
- func (c If) Int8(a, b int8) int8
- func (c If) Rune(a, b rune) rune
- func (c If) String(a, b string) string
- func (c If) Uint(a, b uint) uint
- func (c If) Uint16(a, b uint16) uint16
- func (c If) Uint32(a, b uint32) uint32
- func (c If) Uint64(a, b uint64) uint64
- func (c If) Uint8(a, b uint8) uint8
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IfIf ¶
func IfIf(c bool, a, b interface{}) interface{}
IfIf returns a if c is true, b otherwise.
func NewFloat32 ¶
NewFloat32 returns a pointer to the given float32 value.
func NewFloat64 ¶
NewFloat64 returns a pointer to the given float64 value.
func Pie ¶
func Pie(err error)
Pie is a "panic-if-error" utility: panics if the passed error is not nil. Should not be over-used, but may come handy to write code quickly.
func Wrap ¶
func Wrap(vs ...interface{}) []interface{}
Wrap returns its arguments as a slice.
General use of Wrap is to wrap function calls, so the return values of the "wrapped" function will be available as a slice. Which then can be passed to variadic functions that have other parameters too.
Most notable example is fmt.Printf(). This code doesn't compile:
// Compile-time error! fmt.Printf("Year: %d, month: %d, day: %d", time.Now().Date())
But with the help of this Wrap:
// This is OK! fmt.Printf("Year: %d, month: %d, day: %d", Wrap(time.Now().Date())...)
For details, see https://stackoverflow.com/a/52654950/1705598
Example ¶
ExampleWrap shows how to use the Wrap() function.
now := time.Date(2020, 3, 4, 0, 0, 0, 0, time.UTC) // Note that without Wrap it's a compile-time error. fmt.Printf("Year: %d, month: %d, day: %d\n", Wrap(now.Date())...)
Output: Year: 2020, month: 3, day: 4
Types ¶
type If ¶
type If bool
If is a helper type to form expressive ternary expressions being the concatenation of a type conversion and a method call such as:
i := If(cond).Int(a, b)
For details, see https://stackoverflow.com/a/59375088/1705598