#struct initialization

15 messages · Page 1 of 1 (latest)

cunning rock
#

Hi this is more syntax related question..

Suppose I have an interface as follows:

type ReaderWriter interface {
Reader, Writer
}

is it possible to init such type using a struct literal only?
e.g.



var rw ReaderWriter
var rd cReader // implements Reader
var wr cWriter // implements Writer

rw = (ReaderWriter)(struct{rd, wr})
placid zenith
#

no, since struct{rd, wr} would not implement ReaderWriter

#

an anonymous struct can only implement the interface{} interface since anonymous structs cannot have methods

cunning rock
#

yeah is there any other way where I could init such type using a oneliner and without introducing another type that embbeds the Reader and Writer structs?

placid zenith
#

uh

#

technically you could

#

here's a hint

#
    var x io.ReadWriter = struct {
        io.Reader
        io.Writer
    }{}
cunning rock
#

uhm.. you mean?

    var x io.ReadWriter x := struct {
        cReader
        cWriter
    }{}
placid zenith
#

not that, but i had another mistake

round mantle
gilded escarp
placid zenith
#

thank you for repeating the answer again sunturtle