package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type Response struct {
Status string `json:"status"`
Message string `json:"message"`
Result []struct {
BlockNumber string `json:"blockNumber"`
TimeStamp string `json:"timeStamp"`
Hash string `json:"hash"`
Nonce string `json:"nonce"`
BlockHash string `json:"blockHash"`
TransactionIndex string `json:"transactionIndex"`
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
IsError string `json:"isError"`
TxreceiptStatus string `json:"txreceipt_status"`
Input string `json:"input"`
ContractAddress string `json:"contractAddress"`
CumulativeGasUsed string `json:"cumulativeGasUsed"`
GasUsed string `json:"gasUsed"`
Confirmations string `json:"confirmations"`
} `json:"result"`
}
func getapi(wallet string) ([]byte, error) {
res, err := http.Get("https://api.bscscan.com/api?module=account&action=txlist&address=" + wallet + "&startblock=0&endblock=99999999&sort=asc&apikey=5Q4SS1XHV95SIPSCKK1FKCPGJSMRM2EI1K")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
func j_unm(body []byte) Response {
var r Response
err := json.Unmarshal(body, &r)
if err != nil {
log.Println("Error unmarshalling json data:", err)
}
return r
}
func printitem(r Response, jitem string) []string {
item := []string{}
for _, p := range r.Result {
item = append(item, jitem+":"+p.jitem)
}
return item
}
package main
import "fmt"
func main() {
gettrx, _ := getapi("0xf287E9f40f96C0cbAEAdD29Cf22E6CF5cC66030b")
fmt.Println(printitem(j_unm(gettrx), "BlockNumber"))
}
I want to pass as jitem as variable. I got the following error.
jitem is define as string, could be BlockNumber or TimeStamp
p.jitem undefined (type struct{BlockNumber string "json:\"blockNumber\""; TimeStamp string "json:\"timeStamp\""; Hash string "json:\"hash\""; Nonce string "json:\"nonce\""; BlockHash string "json:\"blockHash\""; TransactionIndex string "json:\"transactionIndex\""; From string "json:\"from\""; To string "json:\"to\""; Value string "json:\"value\""; Gas string "json:\"gas\""; GasPrice string "json:\"gasPrice\""; IsError string "json:\"isError\""; TxreceiptStatus string "json:\"txreceipt_status\""; Input string "json:\"input\""; ContractAddress string "json:\"contractAddress\""; CumulativeGasUsed string "json:\"cumulativeGasUsed\""; GasUsed string "json:\"gasUsed\""; Confirmations string "json:\"confirmations\""} has no field or method jitem)compilerMissingFieldOrMethod
pwhich is an iterator inr.Resultdoes not havejitemas a field. What is shown in above error message is the structure ofp.