packagemainimport"fmt"typenoCopystruct{}// Lock is a no-op used by -copylocks checker from `go vet`.
func(*noCopy)Lock(){}func(*noCopy)Unlock(){}typeSstruct{noCopydataint}funcmain(){varsSss:=sfmt.Println(ss)}
golang 没有禁止对实现sync.Locker接口的对象实例赋值进行报错,只是在使用go vet 做静态语法分析时,会提示错误。
1
2
3
# command-line-arguments
./nocopy.go:19: assignment copies lock value to ss: main.S
./nocopy.go:20: call of fmt.Println copies lock value: main.S