Lesson 01 · Web3 with Go & Polygon

Connect Go to 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.

The win By the end of this lesson you'll run a Go program that connects to Polygon's test network and prints the latest block number straight off the live chain. That's your backend's heartbeat.

Three words you need

Node (RPC node)
A computer running Polygon that keeps a copy of the chain. You don't run one — you send requests to somebody else's over HTTP. That HTTP endpoint is an RPC URL.
ethclient
The Go package (from go-ethereum) that speaks the node's JSON-RPC dialect for you. You call normal Go methods; it does the network talking.
Amoy
Polygon's test network — a full copy of Polygon where the money is fake. Chain id 80002, gas paid in test POL. We live here for the whole course: real code, zero real money.

Set up the project

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.

Write the connection

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
You did it That block number is real and it changes every ~2 seconds. Run the program twice with a short pause — the number climbs. Your Go code is reading a live blockchain.

Two Go details worth noticing

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.

If it fails A timeout or connection error usually means the public RPC is rate-limiting you, not that your code is wrong — just re-run. If it persists, swap the URL for another Amoy endpoint (see References). A chainID that isn't 80002 means you dialed the wrong network.

Check yourself

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?

Primary source · read this next
Ethereum Development with Go — Setting up the Client. The definitive free reference for 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.