README

gox

GoDoc

Package gox contains functions and types which could have been builtin, which could have been part of Go itself.

Reasonable to "dot-import" the package, so identifiers will be directly available:

import (
	"fmt"
	"strconv"

	. "github.com/icza/gox/gox"
)

func main() {
	// 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
}

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IfBool

func IfBool(c bool, a, b bool) bool

IfBool returns a if c is true, b otherwise.

func IfByte

func IfByte(c bool, a, b byte) byte

IfByte returns a if c is true, b otherwise.

func IfFloat32

func IfFloat32(c bool, a, b float32) float32

IfFloat32 returns a if c is true, b otherwise.

func IfFloat64

func IfFloat64(c bool, a, b float64) float64

IfFloat64 returns a if c is true, b otherwise.

func IfIf

func IfIf(c bool, a, b interface{}) interface{}

IfIf returns a if c is true, b otherwise.

func IfInt

func IfInt(c bool, a, b int) int

IfInt returns a if c is true, b otherwise.

func IfInt16

func IfInt16(c bool, a, b int16) int16

IfInt16 returns a if c is true, b otherwise.

func IfInt32

func IfInt32(c bool, a, b int32) int32

IfInt32 returns a if c is true, b otherwise.

func IfInt64

func IfInt64(c bool, a, b int64) int64

IfInt64 returns a if c is true, b otherwise.

func IfInt8

func IfInt8(c bool, a, b int8) int8

IfInt8 returns a if c is true, b otherwise.

func IfRune

func IfRune(c bool, a, b rune) rune

IfRune returns a if c is true, b otherwise.

func IfString

func IfString(c bool, a, b string) string

IfString returns a if c is true, b otherwise.

func IfUint

func IfUint(c bool, a, b uint) uint

IfUint returns a if c is true, b otherwise.

func IfUint16

func IfUint16(c bool, a, b uint16) uint16

IfUint16 returns a if c is true, b otherwise.

func IfUint32

func IfUint32(c bool, a, b uint32) uint32

IfUint32 returns a if c is true, b otherwise.

func IfUint64

func IfUint64(c bool, a, b uint64) uint64

IfUint64 returns a if c is true, b otherwise.

func IfUint8

func IfUint8(c bool, a, b uint8) uint8

IfUint8 returns a if c is true, b otherwise.

func NewBool

func NewBool(b bool) *bool

NewBool returns a pointer to the given bool value.

func NewByte

func NewByte(b byte) *byte

NewByte returns a pointer to the given byte value.

func NewFloat32

func NewFloat32(f float32) *float32

NewFloat32 returns a pointer to the given float32 value.

func NewFloat64

func NewFloat64(f float64) *float64

NewFloat64 returns a pointer to the given float64 value.

func NewInt

func NewInt(i int) *int

NewInt returns a pointer to the given int value.

func NewInt16

func NewInt16(i int16) *int16

NewInt16 returns a pointer to the given int16 value.

func NewInt32

func NewInt32(i int32) *int32

NewInt32 returns a pointer to the given int32 value.

func NewInt64

func NewInt64(i int64) *int64

NewInt64 returns a pointer to the given int64 value.

func NewInt8

func NewInt8(i int8) *int8

NewInt8 returns a pointer to the given int8 value.

func NewRune

func NewRune(r rune) *rune

NewRune returns a pointer to the given rune value.

func NewString

func NewString(s string) *string

NewString returns a pointer to the given string value.

func NewUint

func NewUint(i uint) *uint

NewUint returns a pointer to the given uint value.

func NewUint16

func NewUint16(i uint16) *uint16

NewUint16 returns a pointer to the given uint16 value.

func NewUint32

func NewUint32(i uint32) *uint32

NewUint32 returns a pointer to the given uint32 value.

func NewUint64

func NewUint64(i uint64) *uint64

NewUint64 returns a pointer to the given uint64 value.

func NewUint8

func NewUint8(i uint8) *uint8

NewUint8 returns a pointer to the given uint8 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

func (If) Bool

func (c If) Bool(a, b bool) bool

Bool returns a if c is true, b otherwise.

func (If) Byte

func (c If) Byte(a, b byte) byte

Byte returns a if c is true, b otherwise.

func (If) Float32

func (c If) Float32(a, b float32) float32

Float32 returns a if c is true, b otherwise.

func (If) Float64

func (c If) Float64(a, b float64) float64

Float64 returns a if c is true, b otherwise.

func (If) If

func (c If) If(a, b interface{}) interface{}

If returns a if c is true, b otherwise.

func (If) Int

func (c If) Int(a, b int) int

Int returns a if c is true, b otherwise.

func (If) Int16

func (c If) Int16(a, b int16) int16

Int16 returns a if c is true, b otherwise.

func (If) Int32

func (c If) Int32(a, b int32) int32

Int32 returns a if c is true, b otherwise.

func (If) Int64

func (c If) Int64(a, b int64) int64

Int64 returns a if c is true, b otherwise.

func (If) Int8

func (c If) Int8(a, b int8) int8

Int8 returns a if c is true, b otherwise.

func (If) Rune

func (c If) Rune(a, b rune) rune

Rune returns a if c is true, b otherwise.

func (If) String

func (c If) String(a, b string) string

String returns a if c is true, b otherwise.

func (If) Uint

func (c If) Uint(a, b uint) uint

Uint returns a if c is true, b otherwise.

func (If) Uint16

func (c If) Uint16(a, b uint16) uint16

Uint16 returns a if c is true, b otherwise.

func (If) Uint32

func (c If) Uint32(a, b uint32) uint32

Uint32 returns a if c is true, b otherwise.

func (If) Uint64

func (c If) Uint64(a, b uint64) uint64

Uint64 returns a if c is true, b otherwise.

func (If) Uint8

func (c If) Uint8(a, b uint8) uint8

Uint8 returns a if c is true, b otherwise.