Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare struct field name as "type"

Tags:

struct

go

I am integrating API and parsing its response in a structure. For this I need to declare field name as type as API's response contains key named type. But when I declare type in field name it gives me error:

syntax error: unexpected literal type, expecting field name or embedded type.

I don't know how to declare literal type as struct field name.

My struct is

type Test struct {
    active bool
    name string
    description string
    amount  int
    currency string
    type string
}
like image 252
Bhavana Avatar asked Jan 02 '23 12:01

Bhavana


1 Answers

Update your struct to this , to unmarshal api response you need to export your structure's fields i.e your structs feilds should be in upper case:

type Test struct {
    Active bool `json:"active"`
    Name string  `json:"name"`
    Description string `json:"description"`
    Amount  int `json:"amount"`
    Currency string `json:"currency"`
    Type string `json:"type"`
}

And then try to unmarshal your API response to this struct

like image 91
negi Yogi Avatar answered Jan 05 '23 14:01

negi Yogi