Return different structs

166 views
Skip to first unread message

jbog...@gmail.com

unread,
Jun 28, 2016, 1:43:12 PM6/28/16
to golang-nuts
Hello,

I'm having a hard time figuring out if it's possible to return a function but having different structs as the return type.

I'm talking to a REST API that does something like this:

GET /endpoint?option=1


{
 
"fieldA": 1,
 
"fieldB": 2
}


GET
/endpoint?option=2
{
 
"fieldA": 1,
 
"fieldC": 3
}


So I have a golang code like this.


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"`
}


I would normally do something like this:

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


}


But I need to know if it's possible to return either FirstOption or SecondOption, haven't figure out if it's possible or not and can't find a good solution in Google.

I could probably make a struct that actually mixes SecondOption and FirstOption using the omitempty keyword, but I'm not really fond of that solution.

Thanks for the help,

-- Jose

Shawn Milochik

unread,
Jun 28, 2016, 1:47:51 PM6/28/16
to golang-nuts

You could return an interface, or a []byte.

jbog...@gmail.com

unread,
Jun 29, 2016, 3:41:30 PM6/29/16
to golang-nuts, Sh...@milochik.com
Shawn,

Thanks, it was an error on my side. The function actually returns an array of structs but an interface is not an array, so when I tried to return an array of interfaces, it crashed. I changed it from []interface{} to interface{} and it's working.

Thanks for the help.

Shawn Milochik

unread,
Jun 29, 2016, 3:43:25 PM6/29/16
to golang-nuts
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.


Jose Bogarin Solano

unread,
Jun 29, 2016, 5:12:57 PM6/29/16
to Shawn Milochik, golang-nuts
Shawn,

Thanks, I'll have to see how I can make that interface. Thanks for the guidance.

On 29 June 2016 at 13:42, Shawn Milochik <sh...@milochik.com> wrote:
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.

Shawn Milochik

unread,
Jun 29, 2016, 5:24:41 PM6/29/16
to golang-nuts
I'm thinking about something like this:


I just used Stringer, but you could of course just create your own interface with a function you find useful instead.


jbog...@gmail.com

unread,
Jun 29, 2016, 6:10:43 PM6/29/16
to golang-nuts, sh...@milochik.com
Shaw,

Thanks for the help. I definitively need to improve in my interface understanding. You were really helpful.

Simon Ritchie

unread,
Jun 30, 2016, 4:35:26 AM6/30/16
to golang-nuts
By the way, this is covered by in Donovan & Kernighan's new book The Go Programming Language. On pages 197 and 198 of the first edition, they describe an interface Expr, representing an expression, then five concrete types that satisfy that interface and contain different sorts of expressions and sub expressions. Since they all implement the Expr type, you can write a method that can receive and return any or all of them.

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.

Jose Bogarin Solano

unread,
Jun 30, 2016, 12:42:25 PM6/30/16
to Simon Ritchie, golang-nuts
Simon,

Thanks for the information. Ordering the book at this moment.



Reply all
Reply to author
Forward
0 new messages