I recently started learning Blockchain from an amazing tutorial By freeCodeCamp & Patrick Collins. I feel it’s the best & most up-to-date tutorial about blockchain & Solidity available for free. It starts with blockchain basics and then explains solidity contracts and deploying using a NodeJS Backend.
Being a Golang developer I thought of using the same solidity contract using golang instead of nodeJs. And guess what there is great support for Ethereum golang client you can find it here https://geth.ethereum.org/
I will use the contract from Chapter 5 EtherJs Simple Storage. Will reuse the same contract & deploy it on the golang backend.
Create a project directory and initialize it as a go module
mkdir go-simple-storage-fcc
cd go-simple-storage-fcc
Initialize the project using the go mod command
go mod github.com/kunalrsagar/go-simple-storage-fcc
Open the directory in your favorite IDE, create a new file name SimpleStorage.sol
It’s the same Solidity Contract explained by Patrick, that maintains a list of People and their favorite Number and also has a function to Store a single FavoriteNumber and retrieve it.
The major difference here is instead of Node & npm ecosystem, we will use golang, Solidity & Ethereum. This code is tested on golang 1.19, solidity 0.8.15 version.
Once installation is successful we will use the solc command to compile the Solidity file and generate bin and abi
solc --optimize --abi --bin ./SimpleStorage.sol -o build
The --optimize
flag is optional but helps to compile into gas-efficient binary. On successful completion, the abi and bin files should be generated in the build directory.
the major difference while using geth client is to provide an interface to manage a contract using the abigen command.
mkdir api
Create a directory name api
abigen --abi=./build/SimpleStorage.abi --bin=./build/SimpleStorage.bin --pkg=api --out=./api/SimpleStorage.go
The above command takes in the bin and abi file and generates a Go file SimpleStorage.go
with an interface defined to create, access, and manage functions of the contract. It’s an auto-generated file and should not be edited.
Add below dependency
go get github.com/ethereum/go-ethereum
If you encounter some errors in related/indirect dependencies run the below command
go get all
Let’s make sure Ganache is running and copy the RPC Url & Private Key of any wallet from it. Its explain in great detail by Patrick.

Set RPC_URL & PRIVATE_KEY of wallet as environment variables in using the terminal.
export RPC_URL=<local_ganache_url:port>export PRIVATE_KEY=<Wallet Private key>
The wallet private key can be retrieved by clicking on the key icon on any Wallet Address in the Ganache app
Here is where we create a Go-based backend, we will create a deploy.go
. The file will have the main function as an entry point.
ethclient connects to the blockchain network using RPC_URL. We will create an ECDSA private key by passing it the actual PRIVATE_KEY value from environment variables.
The public key will be extracted from the private key using Public()
function and we need it in ECSDA format for our geth.
In the same way, the address of the wallet and chainID are extracted.
Next, we will need to manually create a Transaction Opts that will have a collection of authorized data (signed by the author of the block) to create a valid Ethereum transaction.
We will then call GenNextTransaction()
function in our main function right after chainID and deploy our contract.
Let’s try and run deploy.go

If you see above 👏🏻 works! 🥳
Next, we will call our Retrieve and Store functions of the SimpleStorage Contract. you can access all contract functions as well as a few more using simpleStorageApi
object.
Let’s run it once again, now you should see 0 as your favoriteNumber. Its a type of *big.Int
in go.

Lastly, let’s set a new favorite number by calling the SimpleStorage contract’s Store function, Since it’s not a Gas-free function, we will make a call to GetNextTransaction
function to get an authorized transaction Ops.
we then call simpleStorageApi.Store function with big.NewInt(20)
You should see an output something like this and yeah! congratulations you just deployed a Smart Contract using golang

Below is the full source code of deploy.go
Github Repo👇🏻
References
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing