Answer the question
In order to leave comments, you need to log in
How to write a generic?
Is there a method how to correctly replace data interface{} with a generic?
func (c BaseController) Message(w http.ResponseWriter, code int, data interface{}) {
c.Respond(w, code, map[string]interface{}{"status": code, "data": data})
}
Message[D any](w http.ResponseWriter, code int, data []D)
{ [key: string]: number | string }
Answer the question
In order to leave comments, you need to log in
type Number interface {
int64 | float64
}
func Message[D Number | string](w http.ResponseWriter, data []D) {}
func Message[D int64 | float64 | string](w http.ResponseWriter, data []D) {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question