go-solc provides an easy way to compile Solidity contracts from Go.
go get github.com/lmittmann/go-solc go-solc automatically downloads the specified version of the Solidity compiler from https://binaries.soliditylang.org and caches it at .solc/bin/.
Example test:
// contract_test.go func TestContract(t *testing.T) { c := solc.New("0.8.21") contract, err := c.Compile("src", "Test", solc.WithOptimizer(&solc.Optimizer{Enabled: true, Runs: 999999}), ) // ... }Example directory structure:
workspace/ ├── .solc/ │ └── bin/ # cached solc binaries │ └── solc_v0.8.21 ├── src/ │ └── test.sol ├── contract_test.go ├── go.mod └── go.sumWarning
This package is pre-1.0. There might be breaking changes between minor versions.