Solana
Ready to call this in production?
Free tier covers personal projects. Pay-as-you-go scales without a card.
Solana
Free tier covers personal projects. Pay-as-you-go scales without a card.
github.com/gagliardetto/solana-go is the most complete Solana SDK for Go, with full coverage of the JSON-RPC 2.0 API, transaction building, and PubSub subscriptions. Add it to your module with go get github.com/gagliardetto/solana-go. The two subpackages you reach for most are rpc, which gives you the HTTP client for queries and transaction submission, and ws, which gives you the WebSocket PubSub client for live account, log, and slot streams.
Call rpc.New(endpoint) to build the HTTP client against https://solana.therpc.io/YOUR_API_KEY, and ws.Connect(ctx, endpoint) against wss://solana.therpc.io/YOUR_API_KEY for subscriptions. Remember that Solana reports balances in lamports — divide by 1e9 to get SOL, since 1 SOL = 1,000,000,000 lamports.
Construct an rpc.Client with your Solana endpoint and YOUR_API_KEY, then read a balance and full account state. GetBalance takes a commitment argument (processed, confirmed, or finalized) — confirmed is a good default that balances speed and safety. The balance comes back in lamports, so divide by 1e9 for SOL; GetAccountInfo returns the account owner (the program that controls the account), its lamports, and its raw data.
To move SOL, build a System program transfer instruction and pack it into a transaction bound to a fresh blockhash from GetLatestBlockhash — Solana transactions expire quickly, so always fetch a recent blockhash right before signing. Sign the transaction with the sender's private key, then broadcast it with SendTransaction, which returns the transaction signature you can poll for confirmation.
ws.Connect opens the Solana PubSub endpoint at wss://solana.therpc.io/YOUR_API_KEY for real-time updates without polling. AccountSubscribe returns a subscription handle: read from it in a loop with Recv, which blocks until the watched account changes and then yields the new lamports and data. Call Unsubscribe (and Close the client) when you are finished to release the server-side stream.