zlacker

[parent] [thread] 0 comments
1. bccdee+(OP)[view] [source] 2025-08-26 16:55:15
FYI, newer versions of Go do support the following

  type HorsePlay interface {
   Neigh()
  }
  
  type SomeClass[T HorsePlay] struct {
   players []T
  }
  
  func (sc SomeClass[T]) NeighAll() {
   for _, p := range sc.players {
    p.Neigh()
   }
  }
  
  type Horse struct{}
  
  func (h Horse) Neigh() {
   fmt.Println("neigh!")
  }
  
  func main() {
   sc := SomeClass[Horse]{players: []Horse{{}, {}, {}}}
   sc.NeighAll()
  }
[go to top]