'golang - struct in sync map access
I am trying to load struct type data arbitrarily defined in sync map. Is there any convenient way to access the map type by defining (like generic, sync.Map[struct]{})?
package main
import (
"sync"
)
type mystruct struct {
cnt int
}
func (m *mystruct) Add() {
m.cnt++
}
func main() {
m := sync.Map{}
m.Store("a", &mystruct{1})
m.Store("b", &mystruct{1})
v, _ := m.Load("a")
v.Add() // i know v.(*mystruct).Add() will solve problem. but is that really only solution?
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
