I'am writting a script to interrract with a smart contract:
package main
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum"
"github.com/joho/godotenv"
)
var myenv map[string]string
const envLoc = ".env"
func loadEnv() {
var err error
if myenv, err = godotenv.Read(envLoc); err != nil {
log.Printf("could not load env from %s: %v", envLoc, err)
}
}
func main() {
loadEnv()
ctx := context.Background()
client, err := ethclient.Dial(os.Getenv("GATEWAY"))
if err != nil {
log.Fatalf("could not connect to Ethereum gateway: %v\n", err)
}
defer client.Close()
accountAddress := common.HexToAddress("786af135e476c3b6061482e90c6273b8ee78c159")
balance, _ := client.BalanceAt(ctx, accountAddress, nil)
fmt.Printf("Balance: %d\n", balance)
}
I get undefined ethclient and undefined common. The import used to work fine, I think I have used some command to dlet the libraries or I'm using import from another folder
Make sure to import the correct packages. Here's a working example:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ctx := context.Background()
fmt.Println(os.Getenv("GATEWAY"))
client, err := ethclient.Dial(os.Getenv("GATEWAY"))
if err != nil {
log.Fatalf("could not connect to Ethereum gateway: %v\n", err)
}
defer client.Close()
accountAddress := common.HexToAddress("786af135e476c3b6061482e90c6273b8ee78c159")
balance, _ := client.BalanceAt(ctx, accountAddress, nil)
fmt.Printf("Balance: %d\n", balance)
}