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.
Input modes
abigen supports three mutually exclusive input sources. Choose the one that fits your build pipeline.
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
Input flags
Input flags
| Flag | Description |
|---|---|
--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. |
Output flags
Output flags
| Flag | Description |
|---|---|
--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. |
--v2 | Generate 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
2. Generate Go bindings
3. Use the generated bindings
The generated file exposesDeployToken, NewToken, and typed wrappers for every public method.
main.go
Using combined-json output
When a project has multiple contracts or library dependencies, usesolc’s --combined-json flag and feed the result directly to abigen.
