0
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

3
  • it is saying p which is an iterator in r.Result does not have jitem as a field. What is shown in above error message is the structure of p. Commented Jul 25, 2022 at 4:28
  • You should provide complete type definition and what your string parameter format is so that we can understand your code. Commented Jul 25, 2022 at 10:16
  • i add the completed code, if you change p.jitem for p.BlockNumber it will work Commented Jul 25, 2022 at 19:58

2 Answers 2

1

is p.jitem is string? if is this type is convertible to string,then use string(p.jitem)

func printitem(r Response, jItem string) []string {
//item := []string{}
items:= make([]string,0)
for _, p := range r.Result {
    items = append(items, jItem+":"+ string(p.jItem))
}
return items
Sign up to request clarification or add additional context in comments.

Comments

0

do you want this?

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

type Response struct {
    Status  string              `json:"status"`
    Message string              `json:"message"`
    Result  []map[string]string `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
}

func main() {
    gettrx, _ := getapi("0xf287E9f40f96C0cbAEAdD29Cf22E6CF5cC66030b")
    fmt.Println(printitem(j_unm(gettrx), "blockNumber"))
}

5 Comments

For sure p.jitem is not a string, I want to add a variable like p.(variable)
all right, did you solve your problem?
no, that is the porblem is not a string
Thanks so much, is better in this case use map, I dint see it in that way.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.