时间:2021-01-09 11:14:07 | 栏目:Golang | 点击:次
前言
Go中没有原生的禁止拷贝的方式,所以如果有的结构体,你希望使用者无法拷贝,只能指针传递保证全局唯一的话,可以这么干,定义 一个结构体叫 noCopy,要实现 sync.Locker 这个接口
方法如下:
// noCopy may be embedded into structs which must not be copied // after the first use. // // See https://golang.org/issues/8005#issuecomment-190753527 // for details. type noCopy struct{} // Lock is a no-op used by -copylocks checker from `go vet`. func (*noCopy) Lock() {} func (*noCopy) UnLock() {}
然后把 noCopy 嵌到你自定义的结构体里,然后 go vet 就可以帮我们进行检查了。
举个例子:
package main import ( "fmt" ) type noCopy struct{} func (*noCopy) Lock() {} func (*noCopy) Unlock() {} type Demo struct { noCopy noCopy } func Copy(d Demo) { CopyTwice(d) } func CopyTwice(d Demo) {} func main() { d := Demo{} fmt.Printf("%+v", d) Copy(d) fmt.Printf("%+v", d) }
执行一下:
$ go vet main.go # command-line-arguments ./main.go:16: Copy passes lock by value: main.Demo contains main.noCopy ./main.go:17: call of CopyTwice copies lock value: main.Demo contains main.noCopy ./main.go:19: CopyTwice passes lock by value: main.Demo contains main.noCopy ./main.go:23: call of fmt.Printf copies lock value: main.Demo contains main.noCopy ./main.go:25: call of Copy copies lock value: main.Demo contains main.noCopy ./main.go:27: call of fmt.Printf copies lock value: main.Demo contains main.noCopy
总结