Get started with Starknet.go
StarkNet.go is a comprehensive Go library that facilitates interaction with the StarkNet blockchain network. StarkNet is a permissionless, decentralized ZK-Rollup operating as a Layer 2 scaling solution for Ethereum.
Prerequisites
Before you begin, make sure you have:
- Installed Go 1.20 or higher
- Installed Starknet.go
- Basic understanding of Go programming
Overview
Starknet.go enables Go backend applications and WASM frontends to seamlessly connect with Starknet by providing abstractions for the Starknet RPC, account managements and wallet operations.
Creating Your First Starknet.go Application
Let's create a simple application that connects to Starknet and retrieves the latest block number.
Step 1: Set up your project
Create a new directory for your project and initialize a Go module:
mkdir my-starknet-app
cd my-starknet-app
go mod init my-starknet-app
Step 2: Install dependencies
Add Starknet.go to your project:
go get github.com/NethermindEth/starknet.go
Step 3: Create the main application
Create a new file main.go
with the following code:
package main
import (
"context"
"fmt"
"log"
"github.com/NethermindEth/starknet.go/rpc"
)
func main() {
// Initialize connection to RPC provider
rpcUrl := "https://free-rpc.nethermind.io/mainnet-juno/"
client, err := rpc.NewProvider(rpcUrl)
if err != nil {
log.Fatal(err)
}
fmt.Println("Established connection with the client")
// Get the latest block number
blockNumber, err := client.BlockNumber(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Latest block number: %d\n", blockNumber)
}
Step 4: Run the application
Run your application:
go run main.go
You should see output similar to:
Established connection with the client
Latest block number: 123456
Next Steps
Now that you have a basic application working, you can explore more features:
- Client Configuration - Learn how to configure your Starknet client
- Client Methods - Explore available methods for interacting with Starknet
- Examples - See more examples of using Starknet.go
Troubleshooting
If you encounter any issues:
- Make sure your Starknet node URL is correct and accessible
- Check that you have the latest version of Starknet.go installed
- Verify your Go version meets the minimum requirement (1.20 or higher)
- Check the documentation for more detailed setup instructions