You would typically define the array type as an array of some interface
that is implemented by both purchase and coupon. You can then use a
type-switch or type-assertion to determine/assert the actual type at
runtime, on a per element basis.

If the two types do not share some interface (e.g. common methods), you
could define the array as []interface{}. But this also allows code to
accidentally add *any* kind of value to the array (not just a purchase or a
coupon). So it may be better to create a marker interface -- an unexported
interface with a no-op unexported marker method -- and make purchase and
coupon both implement that interface.


For example:

type checkoutObject interface {
  checkoutObject()
}

type Purchase struct {
    // yadda yadda
}

func (p *Purchase) checkoutObject() {
    // no-op marker method
}


type Coupon struct {
    // yadda yadda
}

func (c *Coupon) checkoutObject() {
    // no-op marker method
}

// assert that Purchase and Coupon implement checkoutObject
var _ checkObject = (*Purchase)(nil)
var _ checkObject = (*Coupon)(nil)


*// Now you can define your array like so:*
*var checkoutItems []checkoutObject*
*// The compiler will only let you add *Purchase and *Coupon*
*// values to the array.*

*// You can use the values at runtime like so:*
*for e := range checkoutItems {*
*    switch e := e.(type) {*
*    case *Purchase:*
*        // handle purchase*
*    case *Coupon:*
*        // handle coupon*
*    default:*
*        panic(fmt.Sprintf("unsupported type: %T", e))*
*    }*
*}*


----
*Josh Humphries*
jh...@bluegosling.com

On Thu, Jan 4, 2018 at 10:24 AM, Tong Sun <suntong...@gmail.com> wrote:

> I need a data struct / solution that I can store mixed data-types into an
> array. How to architect that?
>
>
> Details -- Consider the checkout point at the cashier, the checkout
> process receives the following string of instructions
>
>    1. purchase
>    2. coupon
>    3. purchase
>    4. purchase
>
> I want to store all the above request data into an array, so I don't lost
> the sequence how they arrive, which in turn requests the array element to
> be either purchase or coupon.
>
>
> How to make it happen? Thanks
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to