I have been trying to convert the struct into a map using generics like below
// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
)
type Data interface {
int64 | float64 | string
}
type Server struct {
Name string
ID int32
Enabled bool
}
func ConvertStructToMap[K comparable, V Data](in interface{}) map[string]V {
var inInterface map[string]V
inrec, _ := json.Marshal(in)
json.Unmarshal(inrec, &inInterface)
return inInterface
}
func main() {
s := &Server{
Name: "gopher",
ID: 123456,
Enabled: true,
}
conv := ConvertStructToMap(s)
fmt.Println(conv)
}
But its giving me an error
cannot infer K (prog.go:20:25)
I have just started with go generics ,and what I want is to convert an struct to map using generics .Any help would be appreciated