What is Go Ethereum?
Go Ethereum (Geth) is the official Go implementation of the Ethereum protocol. It's used to run Ethereum nodes, build dApps, and interact with the blockchain using Go.
Installation
macOS
brew install geth
Linux
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
Build from Source
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make geth
Run an Ethereum Node
Start Geth Node
# Mainnet
geth
# Sepolia Testnet
geth --sepolia
# With JSON-RPC
geth --http --http.addr "localhost" --http.port 8545
Go Ethereum API (Geth)
Install Go Library
go get github.com/ethereum/go-ethereum
Connect to Ethereum
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://mainnet.infura.io/v3/YOUR_KEY")
if err != nil {
panic(err)
}
fmt.Println("Connected:", client)
}
Check Balance in Go
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func getBalance(client *ethclient.Client, address string) {
account := common.HexToAddress(address)
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
panic(err)
}
fmt.Println("Balance:", balance.Div(balance, big.NewInt(1e18)), "ETH")
}
Send Transaction in Go
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
)
func sendTransaction(client *ethclient.Client, privateKey string) {
// Parse private key
key, err := crypto.HexToECDSA(privateKey)
if err != nil {
panic(err)
}
// Get nonce
from := crypto.PubkeyToAddress(key.PublicKey)
nonce, err := client.PendingNonceAt(context.Background(), from)
// Create transaction
tx := types.NewTransaction(nonce,
common.HexToAddress("0x123..."),
big.NewInt(1000000000000000000), // 1 ETH
21000,
big.NewInt(50000000000), // 50 Gwei
nil)
// Sign and send
chainID, _ := client.NetworkID(context.Background())
signedTx, _ := types.SignTx(tx, types.NewEIP155Signer(chainID), key)
err = client.SendTransaction(context.Background(), signedTx)
fmt.Println("Tx hash:", signedTx.Hash())
}
Smart Contract in Go
Generate Go Bindings
abigen --sol=MyContract.sol --pkg=mycontract --type=MyContract --out=mycontract.go
Interact with Contract
contract, err := mycontract.NewMyContract(
common.HexToAddress("0x123..."),
client,
)
// Call read method
result, err := contract.MyMethod(nil)
// Call write method
tx, err := contract.MyMethod(&bind.TransactOpts{
From: from,
Signer: signer,
})
Geth Commands
- geth account new - Create new wallet
- geth attach - Attach to running node
- geth console - Start JavaScript console
- geth export - Export blockchain data
- geth import - Import blockchain data
When to Use Go Ethereum
- Backend Services: Building robust dApp backends
- High Performance: When you need maximum throughput
- Enterprise: Production systems requiring reliability
- Node Operations: Running your own Ethereum node
- DeFi Protocols: Trading bots and financial applications