types

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 6 Imported by: 7

README

From one type to another

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNumber added in v0.1.3

func IsNumber(input any) bool

func ToAddress

func ToAddress(input any) common.Address

Convert string into go-ethereum address.

Input must be go-ethereum address or length 42 string, otherwise will return 0x0.

Example
fmt.Println(ToAddress(1))
fmt.Println(ToAddress("111"))
fmt.Println(ToAddress("0xdAC17F958D2ee523a2206206994597C13D831ec7"))
fmt.Println(ToAddress("0xdac17f958d2ee523a2206206994597c13d831ec7"))
Output:

0x0000000000000000000000000000000000000000
0x0000000000000000000000000000000000000000
0xdAC17F958D2ee523a2206206994597C13D831ec7
0xdAC17F958D2ee523a2206206994597C13D831ec7

func ToBigInt

func ToBigInt(input any) *big.Int

Convert number or string into *big.Int.

Input must be number or number string, otherwise will return 0.

Example
fmt.Println(ToBigInt(1))
fmt.Println(ToBigInt(1.4))
fmt.Println(ToBigInt(1.8))
fmt.Println(ToBigInt("2"))
fmt.Println(ToBigInt("2.3"))
Output:

1
1
2
2
<nil>

func ToBool added in v0.1.5

func ToBool(input any) bool

Convert anything to boolean.

func ToDecimal

func ToDecimal(input any) decimal.Decimal

Convert number or string into decimal.Decimal.

Input must be number or number string, otherwise will return 0.

Example
fmt.Println(ToDecimal(big.NewInt(9)))
fmt.Println(ToDecimal(9.9))
fmt.Println(ToDecimal("9.99"))
Output:

9
9.9
9.99

func ToFloat64

func ToFloat64(input any) float64

Convert number or string into float64.

Input must be number or number string, otherwise will return 0.

Example
fmt.Println(ToFloat64(1))
fmt.Println(ToFloat64(1.4))
fmt.Println(big.NewFloat(1.8))
Output:

1
1.4
1.8

func ToHash added in v0.1.6

func ToHash(input any) common_eth.Hash

Convert anything to hash.

func ToInt

func ToInt(input any) int

Convert number, string or bool value into int64, and then convert into int.

func ToInt64

func ToInt64(input any) int64

Convert number, string or bool value into int64.

Input must be number, number string or bool value, otherwise will return 0.

Float number will return the round int.

Can convert strings start by 0x, 0o or 0b to Dec int.

True ==> 1; False ==> 0.

Example
fmt.Println(ToInt64(1.4))
fmt.Println(ToInt64(1.8))
fmt.Println(ToInt64(big.NewInt(2)))
fmt.Println(ToInt64(big.NewFloat(2.6)))
fmt.Println(ToInt64("0x11"))
fmt.Println(ToInt64("0o66"))
fmt.Println(ToInt64(true))
Output:

1
2
2
3
17
54
1

func ToLowerString

func ToLowerString(input any) string

func ToString

func ToString(input any) string

Convert anything to string.

Example
fmt.Println(ToString(1))
fmt.Println(ToString(1.1))
fmt.Println(ToString(true))
Output:

1
1.1
true

func ToUpperString

func ToUpperString(input any) string

Types

type AddressTypes

type AddressTypes interface {
	string | common.Address
}

type BigNumber

type BigNumber interface {
	~*big.Int | ~*big.Float
}

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	Signed | Unsigned
}

All kinds of integer.

type Number

type Number interface {
	OrderedNumber | BigNumber | decimal.Decimal
}

type Ordered

type Ordered interface {
	OrderedNumber | ~string
}

Types that can use <, >, etc.

type OrderedNumber

type OrderedNumber interface {
	Integer | Float
}

Number types that can use <, >, etc.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

All kinds of sighed integer.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

All kinds of unsigned integer.

Jump to

Keyboard shortcuts

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