Golang struct {}{} meaning - go

I am looking at the docs for the chi package. I see something like:
https://github.com/pressly/chi/blob/master/_examples/rest/main.go#L154
data := struct {
*Article
OmitID interface{} `json:"id,omitempty"` // prevents 'id' from being overridden
}{Article: article}
How do I interpret this? 2 parts I don't fully understand
How does the OmitID part prevent id from being set?
What does the {Article: article} part do?

The first {} in the struct definition is for define the field or attribute of that struct.
data := struct {
*Article
OmitID interface{} `json:"id,omitempty"` // prevents 'id' from being overridden
}
So the data is a struct that has fields *Article and OmitID with their respected type.
What does the {Article: article} part do?
the second {} is for defining the value of that field.
{Article: article}
this part is defining the value of Article field.
How does the OmitID part prevent id from being set?
In go you can define any number of field in the struct.
And you can call define it by calling the field and the value with the respected type. for example if I have this struct :
type DriverData struct {
Name string `json:"name"`
Status bool `json:"status"`
Location GeoJson `json:"location"`
}
I can call it like this :
example := DriverData{Name : "SampleName"}
the rest of the field will have zero values based on their respective data types.
You can read about golang Zero Values here

Related

Read top level struct tags

How to get the tags from a struct field in Go? I have a nested struct which I want to pass as an argument to another function and read the tags there. I know that by accessing it as a field is possible, but I am searching for a way to it.
type MyStruct struct {
Nested struct{} `bson:"nested"`
}
func main() {
val := reflect.ValueOf(MyStruct{})
val.Type().Field(0).Tag.Get("bson") // I want to avoid this
val := reflect.ValueOf(MyStruct{}.Nested)
val.Tag???
}
The tag you want to access belongs to MyStruct. If you pass the value of the Nested field, a copy is made which will be completely detached from MyStruct. There's no way to tell if the value passed originates from a field of MyStruct or from another struct, or from any other source (e.g. from a composite literal). So this is not possible.

Why can't we embed slices or maps into Go structs

I learnt that it is not allowed to embed slices or maps into Go structs. But I found two workarounds to this:
Declare the slice or map outside the struct and embed it into the struct
var mySlice []int
type myStruct struct {
mySlice
}
I don't really understand this second workaround, but I found it's called Composition and it's done by just omitting the var keyword when declaring the slice or map within the struct
type myStruct struct {
mySlice []int
}
My first question is, can anyone explain why we can't directly embed slices and maps into a struct?
Second question: using the second workaround, is there any negative performance effect to that?
Third question: why do the first and second workaround work?
Spec: Struct types:
A field declared with a type but no explicit field name is called an embedded field. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.
You can't embed a type like map[int]string, that's an anonymous type. Spec requires a type name. Even if you could embed that, it contains brackets which would make it disqualify from being an identifier, and again, spec says the unqualified type name acts as the field name.
Simply create a new type for it which you can then embed:
type mymap map[int]string
type s struct {
mymap
}
When you embed a type in a struct, you omit the field name. In your second attempt:
type myStruct struct {
mySlice []int
}
You did not omit the field name (it's mySlice), so this is not embedding, it's a regular, named field.

Parse and validate "key1:value1; key2:value2" string to Go struct efficiently?

I have a "key1:value1; key2:value2" like string (string with key:value pattern concated by ;).
Now I wish to parse this string to a Go struct:
type CustomStruct struct {
KeyName1 string `name:"key1" somevalidation:"xxx"`
KeyName2 int `name:"key2" somevalidation:"yyy"`
}
In the above example, the struct tag defines the name of the key in the string and can provide some validation for its corresponding value (it can set a default value if validation fails). For instance, KeyName2 is an int value, so I wish the somevalidation can check whether the KeyName2 satisfy, let's say, greater than 30 and less equal than 100.
And in another senario, I can define another struct CustomStruct2 for string like key3:value3; key4:value4;
How can I archive this kind of requirement efficiently and elegantly?
I'll assume that you can parse the data to a map[string]interface{}.
Use the reflect package to set the fields. Here's the basic function:
// set sets fields in struct pointed to by pv to values in data.
func set(pv interface{}, data map[string]interface{}) {
// pv is assumed to be pointer to a struct
s := reflect.ValueOf(pv).Elem()
// Loop through fields
t := s.Type()
for i := 0; i < t.NumField(); i++ {
// Set field if there's a data value for the field.
f := t.Field(i)
if d, ok := data[f.Tag.Get("name")]; ok {
s.Field(i).Set(reflect.ValueOf(d))
}
}
}
This code assumes that the values in the data map are assignable to the corresponding field in the struct and that the first argument is a pointer to a struct. The code will panic if these assumptions are not true. You can protect against this by checking types and assignability using the reflect package.
playground example

encoding/xml handle unmapped elements

From http://golang.org/pkg/encoding/xml/#Unmarshal
If the XML element contains a sub-element that hasn't matched any of the above rules and the struct has a field with tag ",any",
unmarshal maps the sub-element to that struct field.
I'm having trouble getting the remainder of an XML envelope into my struct (to show that I have an incomplete mapping)
http://play.golang.org/p/mnFqAcguJQ
I know you can use exactly this method with bson.M from the mgo packages using ,inline - but it looks like map[string]interface{} isn't the answer here.
EDIT:
After some additional playing, I've found what I believe to be some additional unexpected behavior.
Switching to []string as a type starts to accept input, but no key/value pairs: http://play.golang.org/p/wCAJeeQa4m
I also planned on adapting encode/xml in order to parse html. I do not see in the documentation that if an element exists more than once, it will save the last instance of it, rather than erroring out: http://play.golang.org/p/0MY__R-Xi3
Here: http://play.golang.org/p/iY8YlxYym0
Since c is something concrete, it shouldn't use ",any", hence it should have a struct definition. C itself contains a list of arbitrary tags, hence it should contain an []Tag xml:'",any"'... now to capture the Tag itself, you need xml.Name to get the tag name and something with ",innerxml".
Finally the result is this:
const xmlString = `<foo><a>1</a><b>2</b><c><c1>3</c1><c2>4</c2></c></foo>`
type Foo struct {
A int `xml:"a"`
B int `xml:"b"`
C Extra `xml:"c"`
}
type Extra struct {
Items []Tag `xml:",any"`
}
type Tag struct {
XMLName xml.Name
Content string `xml:",innerxml"`
}
Or the shorter version:
type Foo struct {
A int `xml:"a"`
B int `xml:"b"`
C struct {
Items []struct {
XMLName xml.Name
Content string `xml:",innerxml"`
} `xml:",any"`
} `xml:"c"`
}
For HTML there is go.net/html. Using xml parser for html will be complicated.

Nested properties for structs with unknown property names?

I'm using JSON to get some values into a variable from an external source.
I have a type like this that json.Unmarshal puts values into:
type Frame struct {
Type string
Value map[string]interface{}
}
var data Frame
After unmarshal, I can access a the type by: data.Type
but if I try doing something like:
if data.Type == "image" {
fmt.Printf("%s\n", data.Value.Imagedata)
}
The compiler complains about no such value data.Value.Imagedata.
So my question is, how do I reference properties in Go that I know will be there depending on some condition?
Doing this works:
type Image struct {
Filename string
}
type Frame struct {
Type string
Value map[string]interface{}
}
But that isn't very flexible as I will be receiving different Values.
json.Unmarshal will do its best to place the data where it best aligns with your type. Technically your first example will work, but you are trying to access the Value field with dot notation, even though you declared it to be a map:
This should give you some form of output:
if data.Type == 'image'{
fmt.Printf("%v\n", data.Value["Imagedata"])
}
… considering that "Imagedata" was a key in the JSON.
You have the option of defining the type as deeply as you want or expect the structure to be, or using an interface{} and then doing type assertions on the values. With the Value field being a map, you would always access the keys like Value[key], and the value of that map entry is an interface{} which you could type assert like Value[key].(float64).
As for doing more explicit structures, I have found that you could either break up the objects into their own types, or define it nested in one place:
Nested (with anonymous struct)
type Frame struct {
Type string
Value struct {
Imagedata string `json:"image_data"`
}
}
Seperate structs
type Frame struct {
Type string
Value value
}
type value struct {
Imagedata string `json:"image_data"`
}
I'm still learning Go myself, so this the extent of my current understanding :-).

Resources