安装 Ganache
Web3 学习之旅,第一步安装 Ganache。

Binance 3D logo. Feel free to contact me through email mariia.shalabaieva@gmail.com. Check out my previous collections “Top Cryptocurrencies”, “Elon Musk” and other 3D images! Photo by Mariia Shalabaieva on Unsplash

安装配置 Ganache

创建 workspace 时,把余额设置多一点,然后点【START】。

初始过程创建 10 个账号。

连接 MetaMask

将自定义网络,把 RPC HTTP://127.0.0.1:7545 填入,然后再创建账号时,选择上面的一个私钥导入,这样就能看到总的刚才我们设置的 eth总量了。

使用 Golang Demo

创建 Golang 项目做一个关联,看看效果。

package main  
  
import (  
    "fmt"  
    "github.com/ethereum/go-ethereum/rpc"    "log")  
  
func main() {  
    client, err := rpc.Dial("HTTP://127.0.0.1:7545")  
    if err != nil {  
       log.Fatalf("Failed to connect to Infura: %v", err)  
    }  
    fmt.Println("Successfully connected to Ganache!")  
    defer client.Close()  
}

一样的,先开始连接上,查看余额:

package main  
  
import (  
    "context"  
    "fmt"    "github.com/ethereum/go-ethereum/common"    "github.com/ethereum/go-ethereum/ethclient"    "log")  
  
func main() {  
    client, err := ethclient.Dial("HTTP://127.0.0.1:7545")  
    if err != nil {  
       log.Fatalf("Failed to connect to Infura: %v", err)  
    }  
    fmt.Println("Successfully connected to Ganache!")  
    account := common.HexToAddress("0x5f7E9f13E46b5eb46BDd7836F8bbf6b9C6F20F2e")  
    balance, err := client.BalanceAt(context.Background(), account, nil)  
    if err != nil {  
       log.Fatal(err)  
    }  
  
    fmt.Println(balance) // 25893180161173005034  
    defer client.Close()  
}


最后修改于 2025-09-17