I try use "sync.mutex" protect my func,but i found that the lock still uses the caller to destroy it.
var mutex sync.mutex
This is error:
//caller use
func a() {
for i := 0; i < 10; i++ {
go b(i)
}
}
//My func
func b(i int) {
mutex.Lock()
fmt.Println(i)
mutex.Unlock()
}
this is success,but destroyed my encapsulation method:
//caller use
func a() {
for i := 0; i < 10; i++ {
mutex.Lock()
go b(i)
}
}
//my func
func b(i int) {
fmt.Println(i)
mutex.Unlock()
}
Thanks