I'm not sure without knowing *how* you want to use []*Shape{}.  Perhaps you 
want Shape to be an interface?

type Shape interface {
    DrawMe()
}

type BoxShape struct ...
func (*BoxShape) DrawMe() { }

type LineShape struct ...
func (*LineShape) DrawMe() { }

shapes := []Shape{}

(Note that interfaces internally carry pointers, so you don't generally 
want to have pointers to interface types)

On Thursday, 17 August 2023 at 13:44:10 UTC+1 Mark wrote:

> Here's something that works but uses `any` which is too broad in scope:
> ```go
> type Shape any // Want to constrain to BoxShape and LineShape only
>
> type BaseShape struct {
> X         int
> Y         int
> PenColor  color
> PenWidth  int
> FillColor color
> }
>
> type BoxShape struct {
> BaseShape
> Width  int
> Height int
> }
>
> type LineShape struct {
> BaseShape
> X2 int
> Y2 int
> }
>
> shapes := []*Shape{}
> ```
> Is there a nice way to create a `Shape` type which will allow me to do 
> `shapes := []*Shape{}` and that _also_ constrains the shapes to `BoxShape` 
> and `LineShape`?
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b2d706b5-71f8-461f-ab5f-7da5b238ec17n%40googlegroups.com.

Reply via email to