Understanding how to retrieve an Ethereum account balance is a foundational skill for blockchain developers working with Go. Whether you're building decentralized applications, analyzing wallet activity, or integrating on-chain data into backend systems, accessing accurate balance information is essential. This guide walks you through retrieving both current and historical Ethereum balances using the Go programming language and the go-ethereum library.
We’ll cover how to connect to an Ethereum node, fetch live and past balances, interpret values in wei and convert them to human-readable ETH, and even check pending balances during transaction processing—all using idiomatic Go code.
Connecting to the Ethereum Network
Before retrieving any balance data, you need a connection to the Ethereum blockchain. This is done via an RPC (Remote Procedure Call) client. The ethclient package from go-ethereum allows Go programs to interact with Ethereum nodes.
In this example, we use a public endpoint provided by Cloudflare (https://cloudflare-eth.com)—ideal for development and lightweight applications:
client, err := ethclient.Dial("https://cloudflare-eth.com")
if err != nil {
log.Fatal(err)
}👉 Learn how to securely interact with blockchain networks using developer tools
This establishes a live connection that enables your application to query real-time blockchain data without running your own node.
Fetching the Current Account Balance
Once connected, retrieving the current balance of any Ethereum address is straightforward using the BalanceAt method. You pass in the account address and a block number—use nil to get the latest balance.
account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f")
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance) // Output: 25893180161173005034 (in wei)Here:
common.HexToAddressconverts a hexadecimal string into an Ethereum address.BalanceAtreturns the balance in wei, the smallest denomination of ETH (1 ETH = 10¹⁸ wei).
This approach works for any externally owned account (EOA) or contract address.
Reading Historical Balances
One powerful feature of Ethereum’s transparency is the ability to query an account’s balance at any past block. This is useful for auditing, analytics, or reconstructing financial histories.
To do this, specify a block number as a big.Int:
blockNumber := big.NewInt(5532993)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balanceAt) // Output: 25729324269165216042This retrieves the exact balance the account held at block 5,532,993. Use historical block numbers to analyze changes over time or verify state during specific events like token sales or hacks.
Converting Wei to Human-Readable ETH
Ethereum natively uses wei for precision, but users expect balances in ETH. Since these numbers are too large for standard floating-point types, Go’s math/big and math/big.Float packages are required:
fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue) // Output: 25.729324269165216041This conversion ensures precision is preserved without rounding errors—critical when dealing with financial data.
💡 Pro Tip: Always perform monetary calculations in wei and only format to ETH for display purposes.
Checking Pending Balances
During transaction processing, an account’s balance may be in flux. To see what the balance will be after pending transactions are confirmed, use PendingBalanceAt:
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
if err != nil {
log.Fatal(err)
}
fmt.Println(pendingBalance) // Includes pending outgoing/incoming transactionsThis reflects unconfirmed state changes and is helpful in wallet interfaces or trading bots where up-to-the-second accuracy matters.
👉 Discover advanced blockchain querying techniques with secure API access
Complete Working Example
Below is the full Go program that demonstrates all the concepts above:
package main
import (
"context"
"fmt"
"log"
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://cloudflare-eth.com")
if err != nil {
log.Fatal(err)
}
account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f")
// Get current balance
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Current Balance (wei):", balance)
// Get balance at specific block
blockNumber := big.NewInt(5532993)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println("Balance at Block 5532993 (wei):", balanceAt)
// Convert to ETH
fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println("Balance in ETH:", ethValue)
// Get pending balance
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
if err != nil {
log.Fatal(err)
}
fmt.Println("Pending Balance (wei):", pendingBalance)
}This standalone script can be compiled and run locally using go run account_balance.go, provided you have Go installed and the go-ethereum library imported.
Frequently Asked Questions (FAQ)
Q: What is the difference between wei and ETH?
A: Wei is the smallest unit of Ether—1 ETH equals 1,000,000,000,000,000,000 wei (10¹⁸). All Ethereum calculations occur in wei to maintain precision.
Q: Can I use this method for ERC-20 token balances?
A: No. BalanceAt only retrieves ETH (native currency) balances. For ERC-20 tokens, you must call the token contract’s balanceOf function using ABI bindings.
Q: Why do I need big.Float instead of float64?
A: Ethereum balances often exceed the precision limits of float64. Using big.Float prevents loss of accuracy when converting large wei values to ETH.
Q: Is it safe to use public RPC endpoints like Cloudflare’s?
A: For read-only operations and development, yes. However, for production systems requiring high availability or private key signing, consider dedicated providers or self-hosted nodes.
Q: How often does the pending balance update?
A: It updates dynamically based on the mempool state of the connected node. There’s no fixed interval—it reflects real-time pending transactions.
Q: Can I monitor balance changes over time programmatically?
A: Yes. By polling BalanceAt at regular intervals or listening to logs via SubscribeFilterLogs, you can track balance movements across blocks.
Core Keywords
- Ethereum account balance Go
- Read ETH balance in Go
- Convert wei to ETH
- Historical blockchain balance
- Pending Ethereum balance
- Go Ethereum development
- Blockchain data retrieval
- Ethereum RPC client
By mastering these techniques, you gain powerful tools for building reliable, data-driven blockchain applications in Go. From simple balance checks to complex historical analysis, the go-ethereum library provides everything needed for robust on-chain interaction.
👉 Access enterprise-grade blockchain infrastructure for your next project