--- title: Ethereum - 自己起一個節點,並透過Golang進行操作 --- ###### tags: `BlockChain` # Ethereum - 自己起一個節點,並用Golang進行操作 用golang進行操作 雖然網上已經有許多類似文章,但是在geth改版後,網上查到的文章命令參數幾乎都是舊的,而且資訊凌亂四散,因此特別寫一短篇紀錄,未來可以給自己參考 ### 架節點 1. 安裝geth >brew install geth 2. 依照自己要運行完整節點或者輕節點下不同參數 >geth > or >geth --syncmode "light" ### 測試 3. 另開cmd輸入下面指令,以連上剛才起的節點 >geth attach 4. 輸入下面指令,應顯示以太坊網路的當前塊號 --如果為0,表示資料尚在同步中 >web3.eth.blockNumber 5. 輸入以下指令確認相關資料,尤其是port號 >admin.nodeInfo ### Golang操作 6. 使用以下程式碼取得帳戶餘額 ``` package main import ( "context" "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" ) func main() { client, err := ethclient.Dial("https://localhost:30303") account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f") balance, err := client.BalanceAt(context.Background(), account, nil) if err != nil { fmt.Println(err) } fmt.Println(balance) } 回應:Post "https://localhost:30303": EOF 0 ``` 此處要注意port號,舊版geth預設8545。 可以修改,也可以直接使用第五步確認到的port ### geth相關文章 以太坊客户端Geth命令用法-参数详解:https://learnblockchain.cn/2017/11/29/geth_cmd_options/ Command-line Options https://geth.ethereum.org/docs/interface/command-line-options
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up