1. 首页
  2. 数据库
  3. 其它
  4. Golang中禁止拷贝的实现代码

Golang中禁止拷贝的实现代码

上传者: 2021-01-04 00:18:31上传 PDF文件 38.45KB 热度 16次
前言 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{} // Lo
下载地址
用户评论