Home > OS >  function to print from from a Json each item using a for loop golang
function to print from from a Json each item using a for loop golang

Time:07-25

func printitem(r Response, jitem string) []string {
item := []string{}
for _, p := range r.Result {
    item = append(item, jitem ":" p.jitem)
}
return item

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

CodePudding user response:

what about type Response and Result? the error likely means p.jitem is not a string?

CodePudding user response:

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
  • Related