How to insert data in to struct - arrays

type Orders struct {
data []struct {
href string `json:"href"`
order_id string `json:"order_id"`
} `json:"data"`
}
How do I insert data in to data array struct in orders struct?
orders.data = append(orders.data, orders.data{ href: r.Host+r.URL.Path+"/"+orderid, order_id: orderid})
it errors. What's wrong?

First see append built-in function.
orders.data is not a type. data is a field with an anonymous struct type of the struct named orders. So you should either name that anonymous struct to something like:
type HrefAndOrderID struct {
href string `json:"href"`
order_id string `json:"order_id"`
}
And use
HrefAndOrderID{"dummy_href", "dummy_order_id"}
when appending.
Otherwise you can again use the same signature of that anonymous struct to append:
orders.data = append(orders.data, struct{href string `json:"href"`; order_id string `json:"order_id"`}{ href: r.Host+r.URL.Path+"/"+orderid, order_id: orderid})

Related

How do I Change the Position of Golang Struct Values?

How would I change the position of the json values?
What Im trying to achieve:
[{"key":"f","value":"f"},{"value":"f","key":"f"}]
Problem:
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
}
func main() {
test := []Struct{ {Key: "test",Value: "wep"}, {Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
Running this code prints [{"key":"test","value":"wep"},{"key":"test","value":"wep"}]
I have also tried doing something like this but it just printed empty values
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
Value2 string `json:"value"`
Key2 string `json:"key"`
}
But how would I be able to switch the position of the key and value field around?
type StructA struct {
Key string `json:"key"`
Value string `json:"value"`
}
type StructB struct {
Value string `json:"value"`
Key string `json:"key"`
}
func main() {
test := []interface{}{
StructA{Key: "test", Value: "wep"},
StructB{Value: "wep", Key: "test"},
}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
https://play.golang.org/p/72TWDU1BMaL
If you're using json.Marshal provided by official package it's not passible to do that. In stead, you can implement your own MarhalJSON method so that you can decide the position of your struct fields.
To the question "can I make a single struct type serialize its fields in a different order or different occasions ?" :
there isn't a simple way to do this.
Regarding struct fields : the code you write shows the assigning operations in a given order, but once your code is compiled, there is no trace of that order anymore.
If you really need this (side note : I would be curious to know about your use case ?), you could create a custom type, with its own .MarshalJSON() method, which would hold a value indicating "fields should be serialized in this order".
If what you need is to send an array of heterogeneous objects, use separate struct types, and an []interface{} array :
type Obj1 struct{
Key string `json:"name"`
Value string `json:"value"`
}
type Obj2 struct{
Value string `json:"value"`
Key string `json:"name"`
}
func main() {
test := []interface{}{ Obj1{Key: "test",Value: "wep"}, Obj2{Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
https://play.golang.org/p/TOk28gL0eSK

Returning a nested structure from a function

I am writing code in which I want to return a nested structure. I wonder how to do that.
static int ORDERID = 0;
struct item
{
struct data
{
int orderid;
char content[10][20];
} details;
struct node *next;
};
typedef struct item product;
So insertion of a new product contains two parts:
details structure
pointer pointing to next product
In the details structure I have to have different types of product description, say:
Shoes with its orderid, and other details in the array of strings (char content[10][10])
Shirt with its orderid, and other details in the array of strings (char content[10][10])
So for the insertion of a new product I need a function to return a details structure i.e the nested structure of the product.
How to do that?
Well, you can actually define the struct data outside of the struct item and then use struct data to define a variable details inside of struct item. That is,
struct data {
int orderid;
char content[10][20];
};
struct item {
struct data details;
struct item *next;
}
And, then you can freely return a struct data object from a function.
EDIT :
If you want to create a function which can return a struct data object you can probably do the following :-
struct data * function_data() {
struct data *someData;
someData->orderid = 16;
someData->content[0] = "Shoes";
return someData;
}
I hope this adds further quality to the answer.
You can use struct data as the return type of a function

Append array in Nested struct to another array in another nested struct

I have a nested struct as below
type TaskList struct {
Entries []struct {
Values struct {
TaskID string `json:"Task ID"`
Summary string `json:"Summary"`
Notes interface{} `json:"Notes"`
} `json:"values"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
} `json:"entries"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
}
And I want to access 1 Entries struct and append that to another TaskList struct. I'm not really sure how I would be able to do that.
I want to do something along the lines of :
firstList.Entries = append(firstList.Entries,secondList.Entries)
But I get incompatible types, any help on this would be great.
try this way:
firstList.Entries = append(firstList.Entries, secondList.Entries...)

How to unmarshal multi dimensional array inside struct

I have following data:-
{"me":[{"id": "0xcfd","Title":"Story of Stackoverflow","Users":[{"id":"1","Name":"MetaBoss"},{"id":"2","Name":"Owner"}],"Tag":"golang,programming"}]}
and I have the following struct:-
type Root struct {
ID string `json:"id,omitempty"`
Title string `json:"Title,omitempty"`
Myuser Users `json:"Users,omitempty"` // Users is struct
Tag string `json:"Tag,omitempty"`
}
type Users struct {
ID string `json:"id,omitempty"`
Name string `json:"Name,omitempty"`
}
To unmarshal the data, I am trying to do following things -
type Unmarh struct {
Me []Root `json:"me"`
}
var r Unmarh
err = json.Unmarshal(response, &r)
while printing r.Me[0].Myuser, I am not able to get data.
I am getting below error -
json: cannot unmarshal array into go struct field Root.Myuser of type User struct {....Users struct data}
It needs Myuser to be multidimensional array type and not Users struct. I have no Idea, how to represent Users multidimensional array inside struct
In the json the Users key is an array and so the corresponding Go field should be a slice.
type Root struct {
ID string `json:"id,omitempty"`
Title string `json:"Title,omitempty"`
Users []User `json:"Users,omitempty"`
Tag string `json:"Tag,omitempty"`
}
https://play.golang.org/p/azE7kPFs02V

Struct with interface type fields

Why is it I can't have this in golang?
type EventDefinition struct {
Name string
EventProperties interface{}
}
Where EventProperties can be one of may types of structs, each struct with different fields. The idea is to have an EventDefinition with EventProperties
type Party struct {
Location string
Hour string
}
or
type Wedding struct {
Bride string
Groom string
Hour string
}
or
type Graduation struct {
Location string
Graduate string
}
Found my problem. The problem was not related to this issue, the problem was
Location : event.Party.Location.(string),
At some point in my implementation I was doing this when Location was a nil interface{}, hence the blowup.
As a response to this, it is possible to do what I mentioned.

Resources