convert struct to struct in golang [duplicate] - go

This question already has answers here:
Assign struct with another struct
(4 answers)
Closed 6 years ago.
trying to figure out what the best option is to convert one struct to another, Example
type user1 struct {
FirstName string
LastName string
UserName string
}
type user2 struct {
FirstName string
LastName string
}

The easiest is probably to simply do something like u2 := user2{FirstName: u1.FirstName, LastName: u1.LastName}. That is assuming you don't mind that the username is lost in the "conversion".
It's possible to build more general converters using reflection, but I would normally try to structure my code so it's not needed.

Related

"invalid recursive type" and "illegal cycle in declaration of" [duplicate]

This question already has answers here:
invalid recursive type in a struct in go
(2 answers)
Closed 11 months ago.
Shortened example of the two custom types below. "Question" contains a "SavedQuestion" and "SavedQuestion" contains a "Question".
Errors:
1. illegal cycle in declaration of SavedQuestion
2. invalid recursive type Question
type Question struct {
ID int `json:"id"`
Name string `json:"name"`
QueryText string `json:"query_text"`
SavedQuestion SavedQuestion `json:"saved_question"`
}
type SavedQuestion struct {
ID int `json:"id"`
Index int `json:"index"`
Name string `json:"name"`
Packages PackageSpecList `json:"packages"`
QueryText string `json:"query_text"`
Question Question `json:"question"`
}
I'm modeling some types to consume a 3rd party API... The 3rd party API's objects are modeled this way. It seems perfectly normal that a "SavedQuestion" is a separate type that essentially contains a "Question" along with a bunch of other data about it... I can see the loop with a "Question" also containing a "SavedQuestion"... but that's what the API does. As you can see from the json tags I'm just trying to model exactly what the API is sending so I can marshal/unmarshal etc..
How do I make this work? I can make the "Question" in the "SavedQuestion" a pointer to a "Question" and the errors go away... however I don't know if I should or need to do that, or if it will actually work as the code is not yet complete enough to run a test...
New gopher confused...
Thanks for any input.
You cannot recursively include one struct within another for the simple reason that the size and memory layout of the struct becomes recursive. Size of Question is some data + size of (SavedQuestion), and size of SavedQuestion is size of some data + size of Question...
However, if you use pointers, the sizing problem is solved:
type Question struct {
ID int `json:"id"`
Name string `json:"name"`
QueryText string `json:"query_text"`
SavedQuestion *SavedQuestion `json:"saved_question"`
}
You need to make sure when to reference question.SavedQuestion, it is not nil. This will work just fine for marshaling/unmarshaling. When you unmarshal, if the JSON does not have a saved_question element, it'll be nil.

One struct multiple json representation [duplicate]

This question already has answers here:
Is it possible to have a struct with multiple JSON tags?
(2 answers)
Closed 3 years ago.
The Problem I am trying to solve is that I have a representation as below:
type Request struct{
ItId string `form:"itId"`
tR string `form:"treason"`
cd string `form:"cdetails"`
}
but I want the above one in such a manner that it can accept:
itId and ItrId both for ItId
It will be something like this:
type Request struct{
ItId string `form:"itId"` || ItId string `form:"ItrId"`
tR string `form:"treason"`
cd string `form:"cdetails"`
}
what can be the possible solution for such a case?
I am not sure which package are you importing that uses form struct tag, but to try and answer the question, I am assuming it is similar to json tag that is imported by encoding/json package.
In short, no it is not possible to have a same struct tag for a field, as answered in this SOF question - https://stackoverflow.com/a/37118633/5353730
The OP in above question was trying to accomplish something like:
type Foo struct {
Name string `json:"name" json:"employee_name"`
Age int `json:"age" json:"-"`
}
...which I assume is what you want to achieve.
If, however, you might've been trying to use different meta tags for the same field, then that is easily supportable as shown in this answer - https://stackoverflow.com/a/18635910/5353730
type Page struct {
PageId string `bson:"pageId" json:"pageId"`
Meta map[string]interface{} `bson:"meta" json:"meta"`
}
P.S. You can always create 2 separate fields, one each for itId and ItrId to solve your problem but then you will need to check for both these fields at the place of usage of struct.

What is the advantage of using a pointer to a string instead of a string in Go [duplicate]

This question already has answers here:
What does the asterisk do in "Go"?
(6 answers)
Closed 5 years ago.
Reviewing some go code I came across this:
Person struct {
Name *string `json:"name"`
}
and then some where I saw:
Animal struct {
Name string `json:"name"`
}
What is the advantage of the pointer here?
The * declares a pointer type. A pointer to a string is sometimes used when decoding JSON to distinguish the following JSON:
JSON value of the Name field
{ } nil
{name: ""} pointer to ""
Without the pointer, it's not possible to distinguish a missing value from a blank value in the decoded result.
If the application does not need to make this distinction, then use the second form shown in the question. It's more convenient.
* means pointer.
In your case, Name is a field of type pointer to string.
See http://www.golang-book.com/books/intro/8
The * is a pointer.
A pointer type denotes the set of all pointers to variables of a given
type, called the base type of the pointer. The value of an
uninitialized pointer is nil.
This is coming from the Go Spec. I would suggest reading it all.

Converting between struct types in Go

So I have a question that I've been trying to figure out for a while and hopefully someone has some insight. Basically, I have two similar, but different structs in Go and I would like to convert between them.
Something like the following:
type A struct {
Name string
}
type B struct {
Name NameStruct
}
type NameStruct struct {
Firstname string
Lastname string
}
In this example,
B.Name == NameStruct{
Firstname: strings.Split(A.Name, " ")[0],
Lastname: strings.Split(A.Name, " ")[1],
}
Basically I would like to be able to define a mapping of fields from one struct to the other as well as a conversion function, so that I can do this without writing all of the code to do the conversion manually.
What I'm imagining is that there might be some sort of Go generator out there that could do something like this. I don't think it is possible using reflection because of the nested structs (although I'm open to suggestions).
Basically, does anyone know of a way to do this that is better than either writing conversion code manually or creating my own generator?

What does mean about the `json:"make"` at the end of Make string in Golang? [duplicate]

This question already has answers here:
What is the usage of backtick in golang structs definition? [duplicate]
(2 answers)
Closed 6 years ago.
I am working on some Golang source code, and confusing on Program syntax as below.
What does mean about the json:"make" at the end of Make string in Golang?
type Vehicle struct {
Make string `json:"make"`
Model string `json:"model"`
Reg string `json:"reg"`
VIN int `json:"VIN"`
Owner string `json:"owner"`
Scrapped bool `json:"scrapped"`
Status int `json:"status"`
Colour string `json:"colour"`
V5cID string `json:"v5cID"`
LeaseContractID string `json:"leaseContractID"`
}
Tags are used by encoding packages like encoding/json or encoding/xml to control how fields are interpreted during encoding and decoding. Uses of tags are already discussed in this thread: What are the use(s) for tags in Go?

Resources