Documentation ¶
Index ¶
- type Int
- func (i Int) Copy() Int
- func (i Int) IsNil() bool
- func (i Int) MarshalJSON() ([]byte, error)
- func (i Int) Readable(decimal int64) float64
- func (i *Int) Safer() *Int
- func (i *Int) Scan(val interface{}) error
- func (i Int) ToInt() *big.Int
- func (i *Int) UnmarshalJSON(text []byte) error
- func (i Int) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Int ¶
func FromBigInt ¶ added in v1.3.2
FromBigInt creates by raw *big.Int
func (Int) Copy ¶
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) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. It converts *big.Int to string integer
func (*Int) Safer ¶ added in v1.3.0
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 ¶
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) UnmarshalJSON ¶
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>
Click to show internal directories.
Click to hide internal directories.