to

package
v0.0.0-...-75af99c Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package to provides a set of useful functions for type conversion.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v interface{}) (bool, error)

Bool convert given 'v' to boolean type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	b, err := to.Bool(0)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", b, b)
}
Output:

false type of bool

func Float32

func Float32(v interface{}) (float32, error)

Float32 convert given 'v' to float32 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	f, err := to.Float32(true)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", f, f)
}
Output:

1 type of float32

func Float64

func Float64(v interface{}) (float64, error)

Float64 convert given 'v' to float64 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	f, err := to.Float64("3.14")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", f, f)
}
Output:

3.14 type of float64

func Int

func Int(v interface{}) (int, error)

Int convert given 'v' to int type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	i, err := to.Int(-1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", i, i)
}
Output:

-1 type of int

func Int16

func Int16(v interface{}) (int16, error)

Int16 convert given 'v' to int16 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	i, err := to.Int16(-1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", i, i)
}
Output:

-1 type of int16

func Int32

func Int32(v interface{}) (int32, error)

Int32 convert given 'v' to int32 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	i, err := to.Int32(-1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", i, i)
}
Output:

-1 type of int32

func Int64

func Int64(v interface{}) (int64, error)

Int64 convert given 'v' to int64 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	i, err := to.Int64(-1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", i, i)
}
Output:

-1 type of int64

func Int8

func Int8(v interface{}) (int8, error)

Int8 convert given 'v' to int8 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	i, err := to.Int8(-1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", i, i)
}
Output:

-1 type of int8

func String

func String(v interface{}) (string, error)

String convert given 'v' to string type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.String(false)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

false type of string

func Uint

func Uint(v interface{}) (uint, error)

Uint convert given 'v' to uint type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.Uint("1")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

1 type of uint

func Uint16

func Uint16(v interface{}) (uint16, error)

Uint16 convert given 'v' to unt16 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.Uint16(0)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

0 type of uint16

func Uint32

func Uint32(v interface{}) (uint32, error)

Uint32 convert given 'v' to uint32 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.Uint32(1)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

1 type of uint32

func Uint64

func Uint64(v interface{}) (uint64, error)

Uint64 convert given 'v' to uint64 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.Uint64(nil)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

0 type of uint64

func Uint8

func Uint8(v interface{}) (uint8, error)

Uint8 convert given 'v' to uint8 type.

Example
package main

import (
	"fmt"

	"github.com/n4x2/zoo/to"
)

func main() {
	u, err := to.Uint8(true)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v type of %T", u, u)
}
Output:

1 type of uint8

Types

This section is empty.

Jump to

Keyboard shortcuts

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