Nested structure

58 views
Skip to first unread message

Денис Мухортов

unread,
Nov 15, 2021, 8:30:08 AM11/15/21
to golang-nuts
Hello everyone, there is a small problem how to get the total price field from the Order structure?
type Order struct {
    OrderUID          string  `json:"order_uid"`
    Entry             string  `json:"entry"`
    InternalSignature string  `json:"internal_signature"`
    Payment           Payment `json:"payment"`
    Items             []Items `json:"items"`
    Locale            string  `json:"locale"`
    CustomerID        string  `json:"customer_id"`
    TrackNumber       string  `json:"track_number"`
    DeliveryService   string  `json:"delivery_service"`
    Shardkey          string  `json:"shardkey"`
    SmID              int     `json:"sm_id"`
}
type Items struct {
    ChrtID     int    `json:"chrt_id"`
    Price      int    `json:"price"`
    Rid        string `json:"rid"`
    Name       string `json:"name"`
    Sale       int    `json:"sale"`
    Size       string `json:"size"`
    TotalPrice int    `json:"total_price"`
    NmID       int    `json:"nm_id"`
    Brand      string `json:"brand"`
}
Haven't seen yet that the type of nested structure is a slice

Brian Candler

unread,
Nov 15, 2021, 9:04:36 AM11/15/21
to golang-nuts
You can't get a *value* out of a *type*.  However if you have a *variable* of type Order, e.g.

var o Order

then you could do for example: o.Items[0].TotalPrice to get the TotalPrice of the first item.

Note: it would be conventional to have "type Item" rather than "type Items", because this type represents a single item.  That is, it would be clearer as:

type Order struct {
    ...
    Items  []Item
    ...
}
type Item struct {
    ...
}

Reply all
Reply to author
Forward
0 new messages