L
L
lookingfor22022-04-15 20:44:19
go
lookingfor2, 2022-04-15 20:44:19

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})
}

Can you do without any?
Message[D any](w http.ResponseWriter, code int, data []D)


For example, describe something by analogy with ts
{ [key: string]: number | string }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2022-04-15
@lookingfor2

type Number interface {
    int64 | float64
}
func Message[D Number | string](w http.ResponseWriter,  data []D) {}

or
func Message[D  int64 | float64 | string](w http.ResponseWriter,  data []D) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question