用Golang开发一个增改查的Dapp记事本

用Golang开发一个增改查的Dapp记事本

1.实现效果

本文参照上一篇 用Truffle开发一个增改查的Dapp记事本 通过Golang使用Gin框架实现了一遍,期间写入私钥后执行添加修改比较方便,不需要通过MetaMask每次确认。GIT源码地址

添加修改
在这里插入图片描述

2.准备工具

solc 和 abigen 工具,参考使用go-ethereum操作简单智能合约

通过 abigen 生成 NoteContract.go文件

abigen -sol contracts/NoteContract.sol -pkg contracts -out contracts/NoteContract.go

因为是本地开发,注意替换下 go.mod 的 replace 路径。

3.合约配置

由于时间关系,我这里引用上篇文章Ganache 私链环境和合约地址,修改 service/NoteService.go 的合约相关配置就直接用上了。

名称 当前值 作用
c.accountAddress 0x97AF9124a95639ded09CF337D1EcB07D3B36e259 账户地址
c.contractAddress 0x97AF9124a95639ded09CF337D1EcB07D3B36e259 合约地址
ethclient.Dial http://127.0.0.1:7545 私链地址
privateKey a6a1d2b6d8972d5b12b87e976822fb52f6fd8b2e6280ad69a94ea70ca16330a6 私钥

4.运行Dapp

在note_on_chain_with_go项目目录下

➜  note_on_chain_with_go git:(master) : go run main.go

5.遇到的坑

Gin框架报:ShouldBindJSON 对 零值 报400错误

Headers were already written. Wanted to override status code 400 with 200

尝试了

Index *int `json:"index" binding:"exists"`

不知道为啥不行,后面直接通过前端传String后端转int暴力解决了。

Index string `json:"index" binding:"required"`

参考:
https://gin-gonic.com/zh-cn/
https://goethereumbook.org/zh/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章