[Smart Contract] Go language calls smart contracts geth

Table of contents

1. Get the abi file

contract interface

After compiling the contract in the remix tool, there will be one abi, copy
and then create a new xx.abi file, and paste the assignment into it

Note: The code has changed, and it will also change after recompiling abi, so be sure to use the latestabi

You can also generate abi through solc:

solc --bin test.sol -o test.abi

2. Install the abigen tool

Used to generate go files,

Download file: github address

Find the main.go file, perform go build, and generate the abigen.exe file.
Then put it here in the bin directory
go file is generated using the abigen tool

abigen  --abi  xx .abi  --pkg  packagename  --type  structname  --out  xx .go

  • abi files are available when remix is ​​deployed
  • pkg specifies the package name of the output file, which is the package name
  • type specifies the contract structure name
  • out specifies the output go file name

3. Remix connects to private chain

gethTo start the private chain, specify rpccorsdomain *, otherwise it cannot be connected in remix.

The selection Web3 Providermethod, the default connection to the private chain port 8545

But you can see that we don’t have an account here
So we can create an account
After creating two
You can see it here.

4. Contract Deployment

  1. Connect to the private chain: select Web3 Providerthe default port number 8545, if geththe startup rpcport number is inconsistent, change it to the same
  2. Deploy the contract

The following errors may occur, and the solutions are listed
as follows: 1 .Error: authentication needed: password or unlock: This kind of error can be used to unlock the account.
Account unlock: personal.unlockAccount(“account address”)

Just unlock it

2 .Returned error: exceeds block gas limit: When this error occurs, look at the gaslimit of the block number, and change it to always during deployment.
Get the block number: eth.blockNumber
Get the detailed information according to the block number: eth.getBlock(0) will see the gasLimit The value of , the gaslimit at deployment time cannot be greater than this

3 .Returned error: insufficient funds for gas * price + value: The deployment of the contract requires a handling fee, which must be earned through mining

No money, to mine to make money.

  • Start mining with the specified number of threads: miner.start(1)
  • Stop mining: miner.stop()
  • Check if mining is in progress: eth.mining true: mining, false: not mining
  • Get an array of account addresses: eth.accounts
  • Get the mining address: eth.coinbase is the first account created by default
  • Check the first account balance: eth.getBalance(eth.accounts[0])
  • Get the balance of the specified account: eth.getBalance(“account address”)

5. Initialize the private chain node genesis block

Create a new genesis.json file with the following content




  • coinbase: Mining account address, fill in casually, you can set it later, generally the first user created by default
  • difficulty: Set the difficulty of the current block. If the difficulty is too high, it will be difficult for CPU mining. Set a lower difficulty here.
  • gasLimit: This value sets the limit on the total consumption of GAS, which is used to limit the sum of the transaction information that the block can contain. Fill in the maximum value.
  • nonce: nonce is a 64-bit random number used for mining. Note that the settings of him and mixhash need to meet the Yellow paper of Ethereum
  • mixhash: used for mining in conjunction with nonce, the hash generated by part of the previous block.
  • parentHash: The hash value of the previous block, because it is a genesis block, so this value is 0extraData:
  • timestamp: set the timestamp of the genesis block
  • alloc: It is used to preset an account and the amount of ether in the account. Because private chain mining is easier, we do not need to preset an account with coins, and we can create it ourselves when we need it.

cmd enters the path where the json file is stored

geth -datadir "fanone" init genesis.json

6. Contract Deployment

You need to pay a handling fee when deploying

7. Invoke

github.com/ethereum/go-ethereum v1.10.0

package main

import (
func main() {

    // connect rpc 
    client,err := ethclient.Dial( "" )

    // Define the account address 
    addr to operate the contract := "0x29794ab2ed6c47faff7ebdd6dcdd71a263e25460" 
    // Convert the string address to common.Address
    common_addr := common.HexToAddress(addr)

    if err != nil {
        panic( "Error connecting to Ethereum contract" )

    // create contract object
    contract_obj,err11 := contract.NewTestAddress(common_addr,client)
    if err11 !=nil {
        panic( "Error creating contract object" )

    fmt.Println(contract_obj.TestAddressCaller) // Caller access function 
    fmt.Println(contract_obj.TestAddressTransactor) // Transactor has function 
    fmt.Println(contract_obj.TestAddressFilterer)   // has no effect 

Leave a Comment

Your email address will not be published. Required fields are marked *