Skip to main content
abigen reads Solidity ABI JSON, raw bytecode, or compiler output and generates a Go source file containing a fully typed struct, a Deploy function, and method wrappers for every contract entry point. The result lets you interact with on-chain contracts from Go without writing any low-level ABI encoding by hand.

Installation

abigen ships as part of the standard go-ethereum build.
1

Build all go-ethereum tools

git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make all
The binary is placed at ./build/bin/abigen.
2

Or install standalone

go install github.com/ethereum/go-ethereum/cmd/abigen@latest

Input modes

abigen supports three mutually exclusive input sources. Choose the one that fits your build pipeline.
# Requires a pre-compiled .abi file.
# Add --bin <file> to also generate a Deploy function.
abigen --abi token.abi --pkg main --out token.go
The --sol flag was listed in earlier documentation but has been removed from recent versions of abigen. Use a Solidity compiler to produce an ABI file or combined-json output, then pass that to abigen.

Flags

FlagDescription
--abi <path>Path to the contract ABI JSON file. Use - to read from stdin. Mutually exclusive with --combined-json.
--bin <path>Path to the contract bytecode file. Including this generates a Deploy function.
--combined-json <path>Path to the combined-json file produced by the Solidity compiler. Use - for stdin. Mutually exclusive with --abi.
--exc <types>Comma-separated list of contract names to exclude when processing combined-json input.
FlagDescription
--pkg <name>Go package name for the generated file. Required.
--type <name>Struct name for the generated binding. Defaults to the package name.
--out <path>Output file path. Omit to write to stdout.
--alias <mappings>Comma-separated function/event renames, e.g. original1=alias1,original2=alias2.
--v2Generate v2 bindings (new API style).

Example: ERC-20 token

This walkthrough shows how to compile a minimal ERC-20 contract, generate bindings, and use them in a Go program.

1. Compile the contract

Token.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Token {
    string public name;
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;

    constructor(string memory _name, uint256 _supply) {
        name = _name;
        totalSupply = _supply;
        balanceOf[msg.sender] = _supply;
    }

    function transfer(address to, uint256 amount) public returns (bool) {
        require(balanceOf[msg.sender] >= amount, "insufficient balance");
        balanceOf[msg.sender] -= amount;
        balanceOf[to] += amount;
        return true;
    }
}
solc --abi --bin Token.sol -o build/

2. Generate Go bindings

abigen --abi build/Token.abi --bin build/Token.bin --pkg token --type Token --out token/token.go

3. Use the generated bindings

The generated file exposes DeployToken, NewToken, and typed wrappers for every public method.
main.go
package main

import (
	"context"
	"fmt"
	"math/big"

	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/ethclient"

	"myproject/token"
)

func main() {
	client, err := ethclient.Dial("http://localhost:8545")
	if err != nil {
		panic(err)
	}

	// Load a private key and build a signer.
	key, err := crypto.HexToECDSA("your-private-key-hex")
	if err != nil {
		panic(err)
	}
	auth, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
	if err != nil {
		panic(err)
	}

	// Deploy the contract.
	address, tx, instance, err := token.DeployToken(
		auth,
		client,
		"MyToken",       // name
		big.NewInt(1e18), // totalSupply
	)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Contract deployed at %s (tx: %s)\n", address.Hex(), tx.Hash().Hex())

	// Call a view function — no transaction needed.
	name, err := instance.Name(&bind.CallOpts{Context: context.Background()})
	if err != nil {
		panic(err)
	}
	fmt.Println("Token name:", name)
}

Using combined-json output

When a project has multiple contracts or library dependencies, use solc’s --combined-json flag and feed the result directly to abigen.
# Compile everything into a single JSON file.
solc --combined-json abi,bin contracts/*.sol > combined.json

# Generate bindings for all contracts in the file.
abigen --combined-json combined.json --pkg contracts --out contracts/bindings.go

# Exclude helper contracts you don't need bindings for.
abigen --combined-json combined.json --pkg contracts --exc SafeMath,Ownable --out contracts/bindings.go

Reading ABI from stdin

Pipe ABI JSON directly without writing an intermediate file:
cat token.abi | abigen --abi - --pkg main --out token.go

Build docs developers (and LLMs) love