struct - golang 2 slice types with 1 field -


In light of the fact that Golland does not support unions, what is the best way to obtain:

  Type Fu Structure {word} [string] [] int}  

This can be used only for words or nums One thing I may have tried once used in:

  type foo struct {values ​​[] interface {}}  

But I like something that would restrict the type There are two described or something with pointers.

Use a foo package to hide implementation. For example,

  package foo const (type word = iota + 1 type name) type foo struct {fooType byte words [string string] [int} new word (word [] string) * Foo {Foo (Foo Type: Typed, word: word}} Freak New News (nums [] int) * Foo {Return & amp; FU {Foo Type: TypeNoom, nums: nums}} Function (F * Foo ) Word () [] string {if f.fooType! = Type {return number} return f.deododes} funk (f * fu) names () [] int {if f.fooType! = TypeNums {return nil} Return f.nums}  

ADDENDUM:

Since we package Foo is hiding the implementation of , we can apply it in another way, for example, we can take suggestions and use an interface. And add the [] string [/ code>] the phrase . The value types are used to differentiate between two [] string types. / P>

  package foo type (keyword [] string value speech String Manam [] int) Type Fu Structure {Value Interface {}} Fench New Word (word [] string) * Foo {Return & FU {Value: Value word (word)}} Fench (f * Foo) () [] String {value, ok: fu {returns & amp; Fu} {value: value (phrase)}} function (f * fu) phrase () [] string {value, ok: = f.w.w.E. (Value fee) if! OK {Back Nile} Return Value} Function New News (nums [] int) * Foo {Return & FU {value: valueNums (nums)}} <{Code>   )  
>

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -