Im looking for a way to call a contract that is deployed on the ethereum main network using Go (or JS/anything). From what i can see the foolproof way is to do so over json RPC but i would love to hear of gracefull solutions if anyone has them.

1 Answer 1


Here's a full example of how to query a smart contract using Go and the go-ethereum package.


pragma solidity ^0.4.24;

contract MyContract {
  string public foo;

  constructor() public {
    foo = "bar";


solc --abi MyContract.sol | awk '/JSON ABI/{x=1;next}x' > MyContract.abi
solc --bin MyContract.sol | awk '/Binary:/{x=1;next}x' > MyContract.bin
abigen --bin=MyContract.bin --abi=MyContract.abi --pkg=store --out=MyContract.go


package main

import (


    store "./contracts" // for demo

func main() {
    client, err := ethclient.Dial("https://mainnet.infura.io")
    if err != nil {

    address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
    instance, err := store.NewMyContract(address, client)
    if err != nil {

    version, err := instance.Foo(nil)
    if err != nil {

    fmt.Println(foo) // "bar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.