# Go语言实现查询以太坊代币余额

By [eth.mirror.xyz](https://paragraph.com/@eth-mirror-xyz) · 2021-10-28

---

    import (
        "fmt"
        "github.com/ethereum/go-ethereum/accounts/abi/bind"
        "github.com/ethereum/go-ethereum/common"
        "github.com/ethereum/go-ethereum/ethclient"
        "github.com/honorjoey/ethtest/contracts"
    )
    
    func BalanceToken(Net, tokenAddress, address string) (string, error) {
        client, err := ethclient.Dial(Net)
        if err != nil {
            return "", err
        }
    
        // Golem (GNT) Address
        tokenAddr := common.HexToAddress(tokenAddress)
        instance, err := contracts.NewTobaToken(tokenAddr, client)
        if err != nil {
            return "", err
        }
    
        addr := common.HexToAddress(address)
        bal, err := instance.BalanceOf(&bind.CallOpts{}, addr)
        if err != nil {
            return "", err
        }
        //bal.Div(bal, big.NewInt(1000000000000000000))
        return bal.String(), nil
    }
    
    func main() {
        bal, _ := BalanceToken("ETHNetAddress", "contractAddress", "ethAddress")
        fmt.Println(bal)
    }

---

*Originally published on [eth.mirror.xyz](https://paragraph.com/@eth-mirror-xyz/go-2)*
