bigint

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 6 Imported by: 0

README

PkgGoDev ci codecov

Bigint helper functions for json.Number and sql.Decimal

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int

type Int struct {
	*big.Int
}

func FromBigInt added in v1.3.2

func FromBigInt(i *big.Int) Int

FromBigInt creates by raw *big.Int

func New

func New(i int64) Int

New creates by int64 number

func NewUint added in v1.1.4

func NewUint(i uint64) Int

New creates by uint64 number

func (Int) Copy

func (i Int) Copy() Int

Copy creates new bigint.Int with deeply copy

Example
i := New(100)
j := i.Copy()

i.Add(i.Int, big.NewInt(100))

fmt.Println(i, j)
Output:

200 100

func (Int) IsNil added in v1.2.1

func (i Int) IsNil() bool

IsNil returns is or not nil

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It converts *big.Int to string integer

func (Int) Readable added in v1.3.1

func (i Int) Readable(decimal int64) float64

Readable gets readable float64

func (*Int) Safer added in v1.3.0

func (i *Int) Safer() *Int

Safer converts nil value to new(big.Int)

Example
var a Int
_ = json.Unmarshal([]byte(`{"Field": null}`), &a)

fmt.Println(a.IsNil())
func() {
	defer func() {
		_ = recover()
		fmt.Println("panic!")
	}()
	a.Add(big.NewInt(100), big.NewInt(100))
}()

fmt.Println(a.Safer().IsNil())
fmt.Println(a.IsNil()) // It's already safe

a.Add(big.NewInt(100), big.NewInt(100))
fmt.Println(a)
Output:

true
panic!
false
false
200

func (*Int) Scan

func (i *Int) Scan(val interface{}) error

Scan implements the sql.Scanner interface. It converts decimal(N,0) or integer or NULL to *big.Int

var i Int
_ = db.QueryRow("SELECT i FROM example WHERE id=1;").Scan(&i)

func (Int) ToInt added in v1.3.1

func (i Int) ToInt() *big.Int

ToInt converts to non-nil *big.Int

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(text []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It converts integer or string integer or hex string to *big.Int.

Example
type Object struct {
	Field Int
}

// null
var a Object
_ = json.Unmarshal([]byte(`{"Field": null}`), &a)
fmt.Println(a.Field)

// unsigned integer
var b Object
_ = json.Unmarshal([]byte(`{"Field": 1}`), &b)
fmt.Println(b.Field)

// signed integer
_ = json.Unmarshal([]byte(`{"Field": -1}`), &b)
fmt.Println(b.Field)

// string unsigned integer
var c Object
_ = json.Unmarshal([]byte(`{"Field": "2"}`), &c)
fmt.Println(c.Field)

_ = json.Unmarshal([]byte(`{"Field": "-2"}`), &c)
fmt.Println(c.Field)

// hex string
var d Object
_ = json.Unmarshal([]byte(`{"Field": "0x3"}`), &d)
fmt.Println(d.Field)

// empty hex string
var e Object
_ = json.Unmarshal([]byte(`{"Field": "0x"}`), &e)
fmt.Println(e.Field)

// empty string
var f Object
_ = json.Unmarshal([]byte(`{"Field": ""}`), &f)
fmt.Println(f.Field)
Output:

<nil>
1
-1
2
-2
3
<nil>
<nil>

func (Int) Value

func (i Int) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

var i bigint.Int
// only for nullable field
_ = db.Exec("INSERT INTO example (i) VALUES (?);", i)
i = bigint.New(1024)
_ = db.Exec("INSERT INTO example (i) VALUES (?);", i)

Jump to

Keyboard shortcuts

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