Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Struct field with multiple const types

$
0
0

You can do this with a union interface and generics.

// Declare a type which is both cats and dogstype pettype interface {    cattype | dogtype}// Declare AnimalCount using the union type.type AnimalCount[T pettype] struct {  category Animaltype  subcategory map[T]int32}func main() {    // Instantiate AnimalCount with a concrete type.    dc := AnimalCount[dogtype]{category: dogs, subcategory: map[dogtype]int32{retriever: 2, whippet: 4}}    cc := AnimalCount[cattype]{category: cats, subcategory: map[cattype]int32{siamese: 5, tabby: 10}}    fmt.Println("{}", dc)    fmt.Println("{}", cc)}

Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>