GET /endpoint?option=1
{
"fieldA": 1,
"fieldB": 2
}
GET /endpoint?option=2
{
"fieldA": 1,
"fieldC": 3
}
type FirstOption struct {
FieldA string `json:"fieldA,omitempty"`
FieldB string `json:"fieldB,omitempty"`
}
type SecondOption struct {
FieldA string `json:"fieldA,omitempty"`
FieldC string `json:"fieldC,omitempty"`
}func (option string) FirstOption { //I know this line is bad because I might want to return SecondOption
// Do request, unmarshall json to FirstOption or SecondOption depending on the option string variable
}You could return an interface, or a []byte.
You're welcome. I think that using empty interfaces isn't the best way to do it. I meant (but didn't specify) that you should make an interface that both of your struct types satisfy, and return those instead.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/OnRivRrXE40/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
However, there are some subtleties which can trip you up. Once you start using interfaces rather than concrete types, you are pretty quickly forced into using pointers rather than objects. Go does a pretty good job of hiding some of the details of this, but not a complete job.
Interfaces are also crucial to a lot of testing strategies, because using them allows you to easily mock up objects with the values that you need for your tests.
Unfortunately, this means that somebody new to Go has to get their head around quite a few tricky ideas before they can do anything useful. They are all explained in the book, but you need to read the relevant sections carefully, do a few experiments and then read them again and again until it becomes clear.
The great thing about Kernighan's writing style is that he says everything he needs to say exactly once and using the minimum of words. The thing that often causes his readers problems is that he says everything he needs to say exactly once and using the minimum of words.