Look at the following code snippet
// Prepare some data to insert into the template.
type Recipient struct {
Name, Gift string
Attended bool
}
var recipients = []Recipient{
{"Aunt Mildred", "bone china tea set", true},
{"Uncle John", "moleskin pants", false},
{"Cousin Rodney", "", false},
}
I create a struct with some properties. After create a slice with Recipient type. Does slice recipients keeps value or reference type inside?
It seems to be value type.
A slice is in fact a (kind of) pointer to a portion of an array (see slice usage and internals), so as for arrays, items are values of the specified type.
It keeps whatever you declared your slice to be. In your case, you declared as []Recipient, so it will keep the data as value.
If you declare a slice as []*Recipient, then it will hold references (*Recipient), not Recipient values.
Related
I have two interface objects which I would like to compare against each other. I don't only want to compare if their values are the same, I also want to know whether these two interfaces are referencing the same object or if they're referencing two different objects with equal values.
Is there some way to extract the address an interface references from an interface object? Then I could just compare the two addresses to know whether the two interfaces reference the same object.
If two interfaces have pointer values, then you can simply compare them:
func cmp(v1, v2 interface{}) bool {
return v1==v2
}
func main() {
a:=1
b:=1
c:=&a
cmp(&a,&b) // false
cmp(a, b) // true, compare values
cmp(c, &a) // true
}
Be careful here.
Two different interface values can never "reference" the "same object" as an interface value always contains a copy of the value you wrap in the interface value. Variable identity (your "same object") would be "equal address" which is undefined for values wrapped in an interface value as these wrapped values are not addressable. So a clear no to your question.
But of course you can store a pointer to your value in the interface value iff the pointer type satisfies the interface.
It is best avoided to talk about "object" and "reference". Go has values of certain types. Some values are addressable. You can store addresses of addressable values in appropriately typed pointer variables.
I am sorry for that simple question I have the following code
// package p
package p
// U type
type U struct {
A, B int
c int
} // A and B are exported only
// main
package main
import (
"fmt"
"./p"
)
func main() {
pp := p.U{A: 3, B: 4}
uu := p.U{3, 5} // the error resulted from this line
fmt.Println(pp)
fmt.Println(uu)
}
when I try to compile I get an error: too few values in p.U literal. I'm expecting that there no need to add c value. when I try to add the c value I get another expected error: implicit assignment of unexported field 'c' in p.U literal
You must keep the following rules in mind while creating a struct literal:
A key must be a field name declared in the struct type.
An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared.
An element list that contains keys does not need to have an element for each struct field. Omitted fields get the zero value for that field.
A literal may omit the element list; such a literal evaluates to the zero value for its type.
It is an error to specify an element for a non-exported field of a struct belonging to a different package.
In golang, there's to ways to instantiate structs: with keys and without keys.
When instantiating with keys, you would write the value of each field name next to the field name like such:
type Employee struct {
name string
age int
boss bool
}
employee := Employee{name: "John", age: 30}
When instantiating without keys, you just write the value of each field without writing the field names like such:
type Employee struct{
name string
age int
boss bool
}
employee := Employee{"John", 30, false}
As you might've noticed, when instantiating with keys, you don't have to specify a value for each field (you don't have to include a value for boss). This is because, since you're only giving values to specific fields, golang can assume the values for the other fields.
On the other hand, when instantiating without keys, you do have to specify a value for each field since, otherwise, if you didn't specify a value for each field, golang would not be able to assume which value goes to which field.
So, long story short, you can only instantiate structs without keys if you specify values for each field. Otherwise, you have to use keys and allow golang to assume the default values for the other fields.
I am new to golang and I have one issue which I think community can help me to solve it.
I have one data structure like below
type ParentIDInfo struct {
PCOrderID string `json:"PCorderId",omitempty"`
TableVarieties TableVarietyDC `json:"tableVariety",omitempty"`
ProduceID string `json:"PRID",omitempty"`
}
type PCDCOrderAsset struct {
PcID string `json:"PCID",omitempty"`
DcID string `json:"DCID",omitempty"`
RequiredDate string `json:"requiredDate",omitempty"`
Qty uint64 `json:"QTY",omitempty"`
OrderID string `json:"ORDERID",omitempty"`
Status string `json:"STATUS",omitempty"`
Produce string `json:"Produce",omitempty"`
Variety string `json:"VARIETY",omitempty"`
Transports []TransportaionPCDC `json:"Transportaion",omitempty"`
ParentInfo []ParentIDInfo `json:"ParentInfo",omitempty"`
So I have issue to access the PCOrderID which inside the []ParentIDInfo . I have tried below however I getting error as "pcdcorder.ParentInfo.PCOrderID undefined (type []ParentIDInfo has no field or method PCOrderID)"
keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo.PCOrderID)
Any help will be very good
Thanks in advance
PCDCOrderAsset.ParentInfo is not a struct, it does not have a PCOrderID field. It's a slice (of element type ParentIDInfo), so its elements do, e.g. pcdcorder.ParentInfo[0].PCOrderID.
Whether this is what you want we can't tell. pcdcorder.ParentInfo[0].PCOrderID gives you the PCOrderID field of the first element of the slice. Based on your question this may or may not be what you want. You may want to append all IDs (one from each element). Also note that if the slice is empty (its length is 0), then pcdcorder.ParentInfo[0] would result in a runtime panic. You could avoid that by first checking its length and only index it if its not empty.
In case you'd want to add ids of all elements, you could use a for loop to do that, e.g.:
for i := range pcdorder.ParentInfo {
keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo[i].PCOrderID)
}
I am trying to build a fairly generalizable service to create graphs in arangodb.
In my ReadVertex() function, I would like to be able to read a property with arbitrary fields of a vertex and read it into a variable, preferably a string without having to create a specific struct with the json tags hardcoded for particular properties.
I have no problem inserting arbitrary objects as a property of a vertex, and I've tried reading them into a string, but it always comes up blank.
An example of what I would like to accomplish: if a vertex has a property: "Prop1": {"Nested1": true, "Nested2": false}, and a property "Prop2": "Just a string", in my ReadVertex() function I would like to be able to do something along the lines of
// query the database and get a cursor containing prop1 and prop2
var prop1 someType
cursor.ReadDocument(ctx, &prop1)
fmt.Println(prop1.String()) // should print {"Prop1": {"Nested1": true, "Nested2": false}}
var prop2 someType
cursor.ReadDocument(ctx, &prop2)
fmt.Println(prop2.String()) // should print {"Prop2": "Just a string"}
And I would like this to work having prop1 and prop2 be of the same type.
So it turns out the answer was a lot more simple than I thought it would be. This works if prop1 and prop2 are type interface{}, it reads the data in as a map, then as long as the keys in the map are all strings (which they are in my case), to turn it into a json string I can just use json.Marshal() and cast the returned byte array as a string.
I have two structs that represent models that will be inserted into a mongodb database. One struct (Investment) has the other struct (Group) as one of its fields.
type Group struct {
Base
Name string `json:"name" bson"name"`
}
type Investment struct {
Base
Symbol string `json:"symbol" bson:"symbol" binding:"required"`
Group Group `json:"group" bson:"group"`
Fields bson.M `json:"fields" bson:"fields"`
}
The problem I'm having is that in the Investment model, Group is not required. If there is no group, I think its better for it to not be inserted in the db. Whats the best way to handle a db model such as this in Go?
tl;dr: Use ,omitempty, and if you need to worry about the difference between a zero value and null/not specified, do what the GitHub API does and use a pointer.
Both json and bson support the ,omitempty tag. For json, "empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero" (json docs). For bson, ,omitempty means "Only include the field if it's not set to the zero value for the type or to empty slices or maps", and zero values include empty strings and nil pointers (bson docs).
So if you really need a Group struct, you can put a *Group in instead, and it won't be stored when the pointer is nil. If Investment only needs to hold the group's name, it's even simpler: "" as group name keeps a group key from being stored.
bson defaults to using the lowercased field name already so you can omit that from the struct tag when they match. json will default to the Capitalized name, so specify the lowercase name in a tag if you need lowercase.
So, best case, maybe you can just use:
type Investment struct {
Base
Symbol string `json:"symbol" binding:"required"`
Group string `json:"group,omitempty" bson:",omitempty"`
Fields bson.M `json:"fields"`
}
If you ever run into fields where the zero value for the type ("", 0, false, etc.) is distinct from "not specified", you can do what the GitHub API does and put pointers in your structures--essentially an extension of the *Group trick.
Avoid strut fields to marshal if they are empty -
A struct field may be primitive type(string, int, bool etc) or even an another struct type.
So sometimes we don't want a struct's field to
go in json data(may to database insertion or in external api call) if they are empty
Example:
type Investment struct {
Base
Symbol string `json:"symbol" bson:"symbol" binding:"required"`
Group Group `json:"group" bson:"group"`
Fields bson.M `json:"fields" bson:"fields"`
}
If we want that Symbol and Group might contain empty values(0, false, nil pointer, zero size interface/struct) then we can avoid them in json marshaling like below.
type Investment struct {
Base
Symbol string `json:"symbol,omitempty" bson:"symbol,omitempty" binding:"required"`
Group *Group `json:"group,omitempty" bson:"group,omitempty"`
Fields bson.M `json:"fields" bson:"fields"`
}
Her "Group" field is pointer to Group struct and whenever it will point to nil pointer it will be omitted from json marshaling.
And obviously we would be filling values in Group field like below.
// declared investment variable of type Investment struct
investment.Group = &groupData