泛型demo

package other

import (
    "fmt"
    "sync"
    "testing"
)

type ConcurrentMapShared[V any] struct {
    items        map[string]V
    sync.RWMutex // Read Write mutex, guards access to internal map.
}

func NewCM[V any]() ConcurrentMapShared[V] {
    m := &ConcurrentMapShared[V]{items: make(map[string]V)}
    return *m
}

func TestName(t *testing.T) {

    m := NewCM()
    m.items["hah"] = interface{}(1)

    fmt.Println(m)
}

//type ConcurrentMapShared struct {
//  items        map[string]interface{}
//  sync.RWMutex // Read Write mutex, guards access to internal map.
//}
//
//func NewCM() ConcurrentMapShared {
//  m := &ConcurrentMapShared{items: make(map[string]interface{})}
//  return *m
//}
//
//func TestName(t *testing.T) {
//
//  m := NewCM()
//  m.items["hah"] = interface{}(1)
//
//  fmt.Println(m)
//}
//

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注