Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在golang进行线程安全预检查_Go - Fatal编程技术网

如何在golang进行线程安全预检查

如何在golang进行线程安全预检查,go,Go,上面是我在golang中的方法A(),我想如果这是一个并发场景,可能会导致竞争条件,如何避免它?谢谢你在第一行添加互斥锁,然后再添加延迟解锁?你在找什么? type ServiceDM struct { ServiceData *XXType } func (dm *ServiceDM) A() (*XXType, error) { if dm.ServiceData != nil { return ServiceDM.ServiceData, nil }

上面是我在golang中的方法A(),我想如果这是一个并发场景,可能会导致竞争条件,如何避免它?谢谢你

在第一行添加互斥锁,然后再添加延迟解锁?你在找什么?
type ServiceDM struct {
   ServiceData *XXType
}
func (dm *ServiceDM) A() (*XXType, error) {
     if dm.ServiceData != nil {
       return ServiceDM.ServiceData, nil
     }
    // call some other services to get serviceData
    // do some logic
    ServiceDM.ServiceData = response.ServiceData
    return ServiceDM.ServiceData, nil
}