var m sync.Mutex
//sync.Mutex 是一个互斥锁,可以由不同的协程加锁和解锁。//sync.Mutex 是 Go 语言标准库提供的一个互斥锁//当一个协程(goroutine)获得了这个锁的拥有权后,其它请求锁的协程(goroutine)就会阻塞在 Lock() 方法的调用上,直到调用 Unlock() 锁被释放。var set =make(map[int]bool,0)funcprintOnce(num int){
m.Lock()defer m.Unlock()if_, exist := set[num];!exist {
fmt.Println(num)}
set[num]=true}funcmain(){for i :=0; i <10; i++{goprintOnce(100)}
time.Sleep(time.Second)}
package geecache
// A ByteView holds an immutable view of bytes.type ByteView struct{
b []byte}// Len returns the view's lengthfunc(v ByteView)Len()int{returnlen(v.b)}// ByteSlice returns a copy of the data as a byte slice.func(v ByteView)ByteSlice()[]byte{returncloneBytes(v.b)}// String returns the data as a string, making a copy if necessary.func(v ByteView)String()string{returnstring(v.b)}funccloneBytes(b []byte)[]byte{
c :=make([]byte,len(b))copy(c, b)return c
}