How do I determine a slice to make sure it only accepts float32 or uint32?

31 views Asked by At

I have a field in my struct that stores either a floating point array or an integer array, but I can't use generics to make sure that this field can take both types, normally they would only take one type

I can only use 'any' to make sure I can accept 'float32' or 'uint32', but I need to make sure I can't accept any type other than 'float32' & 'uint32', I tried asking ai, and ai told me that golang's slices can't accept two and more types

type AnimKeyframe[T float32 | int32] struct {
    Frame  uint32 //索引
    Vector []T
    InTan  []any
    OutTan []any
}

func test[T float32 | int32]() {
    // t := AnimKeyframe[T]{}
    t := new(AnimKeyframe[T])
    t.Vector[0] = float32(1)//error
    t.Vector[1] = int(1)//error
    t.InTan  [0] = float32(1)//no error
    t.InTan  [1] = int(1)//no error
}
0

There are 0 answers