Go 語言 big.Int

王一洋發表於2018-09-27

簡介

math/big 作為 Go 語言提供的進行大數操作的官方庫,在以太坊 Ethereum 專案中作為 currency 的型別表示得到了廣泛的使用,這篇文章主要介紹該庫的使用。

官方包解析

在官方的 math/big 包中,Int 型別定義如下:

// An Int represents a signed multi-precision integer.
// The zero value for an Int represents the value 0.
type Int struct {
    neg bool // sign
    abs nat  // absolute value of the integer
}

生成 Int 型別的方法為 NewInt(),如下:

// NewInt allocates and returns a new Int set to x.
func NewInt(x int64) *Int {
    return new(Int).SetInt64(x)
}

可見,NewInt() 函式只對 int64 有效,其他型別必須先轉成 int64 才行。

但是,官方還提供了許多 Set 函式,可以方便的把其他型別的整形存入 Int ,因此,我們可以先 new(int) 然後再呼叫 Set 函式。

// SetInt64 sets z to x and returns z.
func (z *Int) SetInt64(x int64) *Int {
    neg := false
    if x < 0 {
        neg = true
        x = -x
    }
    z.abs = z.abs.setUint64(uint64(x))
    z.neg = neg
    return z
}
​
// SetUint64 sets z to x and returns z.
func (z *Int) SetUint64(x uint64) *Int {
    z.abs = z.abs.setUint64(x)
    z.neg = false
    return z
}
​
// Set sets z to x and returns z.
func (z *Int) Set(x *Int) *Int {
    if z != x {
        z.abs = z.abs.set(x.abs)
        z.neg = x.neg
    }
    return z
}

用法示例

func main() {
    big1 := new(big.Int).SetUint64(uint64(1000))
    fmt.Println("big1 is: ", big1)
    
    big2 := big1.Uint64()
    fmt.Println("big2 is: ", big2)
}

除了上述的 Set 還是,big 庫還提供了一個 SetString() 函式,可以指定進位制數,比如二進位制、十進位制或者十六進位制等!

// SetString sets z to the value of s, interpreted in the given base,
// and returns z and a boolean indicating success. The entire string
// (not just a prefix) must be valid for success. If SetString fails,
// the value of z is undefined but the returned value is nil.
//
// The base argument must be 0 or a value between 2 and MaxBase. If the base
// is 0, the string prefix determines the actual conversion base. A prefix of
// ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a
// ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10.
//
func (z *Int) SetString(s string, base int) (*Int, bool) {
    r := strings.NewReader(s)
    if _, _, err := z.scan(r, base); err != nil {
        return nil, false
    }
    // entire string must have been consumed
    if _, err := r.ReadByte(); err != io.EOF {
        return nil, false
    }
    return z, true // err == io.EOF => scan consumed all of s
}

用法示例

func main() {
    big1 := new(big.Int).SetString("1000", 10)
    fmt.Println("big1 is: ", big1)
    
    big2 := big1.Uint64()
    fmt.Println("big2 is: ", big2)
}

uint64, int64 等普通型別與 big.Int 型別的轉換
如上,直接呼叫 big 庫提供的 Int64(), Uint64() 等函式就可以進行轉換。

Int 物件上的運算函式
Mul(im, in)
Add(ip, im)
Div(ip, io)

 

例如以太坊的wei轉ether,就需要16進位制轉換,參考:

// SetString sets z to the value of s, interpreted in the given base,
// and returns z and a boolean indicating success. The entire string
// (not just a prefix) must be valid for success. If SetString fails,
// the value of z is undefined but the returned value is nil.
//
// The base argument must be 0 or a value between 2 and MaxBase. If the base
// is 0, the string prefix determines the actual conversion base. A prefix of
// ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a
// ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10.
//
func (z *Int) SetString(s string, base int) (*Int, bool) {
    r := strings.NewReader(s)
    if _, _, err := z.scan(r, base); err != nil {
        return nil, false
    }
    // entire string must have been consumed
    if _, err := r.ReadByte(); err != io.EOF {
        return nil, false
    }
    return z, true // err == io.EOF => scan consumed all of s
}

轉換程式碼:

func TestGeth3() {

	client, err := rpc.Dial("http://localhost:8545")
	if err != nil {
		fmt.Println("rpc.Dial err", err)
		return
	}

	var account []string
	err = client.Call(&account, "eth_accounts")

	for i, x := range account {
		fmt.Printf("%d ----> %s  \n", i, x)
	}
	var result string

	err = client.Call(&result, "eth_getBalance", account[0], "latest")
	fmt.Println(result)

	n := new(big.Int)
	n, ok := n.SetString(result, 0)
	if ok {
		fmt.Println(n)
	} else {
		fmt.Println("SetString: error")
		return
	}

	fmt.Printf("account: %s , balance: %s  wei \n ", account[0], n)

}

參考:

https://golang.org/pkg/math/big/