package main
import (
"fmt"
"sync"
)
type myMap struct {
m map[string] interface {}
sync.Mutex
}
func (m *myMap) push(key string, e interface {}) interface {} {
m.Lock()
defer m.Unlock()
if v,exist := m.m[key] ; exist {
return v;
}
m.m[key] = e
return nil
}
func (m *myMap) pop(key string) interface {} {
m.Lock()
defer m.Unlock()
if v,exist := m.m[key] ; exist {
m.m[key] = nil, false
return v
}
return nil
}
func newMap() *myMap {
return &myMap { m: make(map[string] interface {}) }
}
func main() {
m := newMap()
fmt.Println(m.push("hello","world"))
fmt.Println(m.push("hello","world"))
fmt.Println(m.pop("hello"))
fmt.Println(m.pop("hello"))
}
云风10几年前初识 go 已经比很多有很久经验写 go 的同学理解差不多了,体验到了人跟神的差距。。。
import (
"fmt"
"sync"
)
type myMap struct {
m map[string] interface {}
sync.Mutex
}
func (m *myMap) push(key string, e interface {}) interface {} {
m.Lock()
defer m.Unlock()
if v,exist := m.m[key] ; exist {
return v;
}
m.m[key] = e
return nil
}
func (m *myMap) pop(key string) interface {} {
m.Lock()
defer m.Unlock()
if v,exist := m.m[key] ; exist {
m.m[key] = nil, false
return v
}
return nil
}
func newMap() *myMap {
return &myMap { m: make(map[string] interface {}) }
}
func main() {
m := newMap()
fmt.Println(m.push("hello","world"))
fmt.Println(m.push("hello","world"))
fmt.Println(m.pop("hello"))
fmt.Println(m.pop("hello"))
}
云风10几年前初识 go 已经比很多有很久经验写 go 的同学理解差不多了,体验到了人跟神的差距。。。