Lesson 01 · Web3 with Go & Polygon
Your client's tokenized-property backend has exactly one thing it must do before anything else: talk to the blockchain. It can't mint a share, read a balance, or record a sale until a Go process is holding a live connection to a Polygon node. That connection is the foundation everything in this course stands on — so we build it first, today, and prove it works.
ethclientgo-ethereum) that speaks the node's JSON-RPC
dialect for you. You call normal Go methods; it does the network talking.80002, gas paid in test POL. We live here for the
whole course: real code, zero real money.You need Go installed (go version should print 1.21+). Then, in a terminal:
terminal
mkdir polygon-backend && cd polygon-backend
go mod init polygon-backend
go get github.com/ethereum/go-ethereum
That last command downloads go-ethereum and records it in your
go.mod. It's a big library — give it a moment.
main.go
package main
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
// Dial opens the connection to a Polygon (Amoy) RPC node.
client, err := ethclient.Dial("https://rpc-amoy.polygon.technology/")
if err != nil {
log.Fatalf("could not connect: %v", err)
}
defer client.Close()
ctx := context.Background()
// Which chain did we actually reach? Should be 80002 (Amoy).
chainID, err := client.ChainID(ctx)
if err != nil {
log.Fatalf("could not read chain id: %v", err)
}
// The latest block — proof we're reading live chain state.
block, err := client.BlockNumber(ctx)
if err != nil {
log.Fatalf("could not read block number: %v", err)
}
fmt.Printf("connected to chain %s — latest block %d\n", chainID, block)
}
Run it:
terminal
go run main.go
You should see something like:
connected to chain 80002 — latest block 24518903
Every chain-reading method takes a context.Context as its first argument.
context.Background() is the plain "no deadline, no cancellation" context —
fine for now. Later you'll pass one with a timeout so a slow node can't hang your
server. This ctx-first pattern is everywhere in ethclient.
Notice chainID isn't an int. Ethereum numbers can be astronomically
large (a wallet balance is measured in units of 10-18 of a coin), so
go-ethereum uses *big.Int for them. We printed it with
%s because *big.Int knows how to render itself as text. Get
comfortable seeing big.Int — it's the currency of this whole ecosystem.
chainID that isn't
80002 means you dialed the wrong network.
No peeking at the code — recall from memory. That effort is what makes it stick.
1. Which go-ethereum function opens the connection to a node?
2. What is the chain id of the Polygon Amoy testnet?
3. Chain methods like BlockNumber take which type as their first argument?
ethclient; the next chapters (reading
blocks & accounts) are exactly where we go in Lesson 02.
I'm your teacher — ask me anything that's unclear. Stuck on go get? Curious
what a "block" actually contains, or why gas exists? Want to see the balance-reading
version now? Just ask.