当前位置: 首页 > news >正文

可以自己做网站吗邯郸做网站的

可以自己做网站吗,邯郸做网站的,做网络推广的方法,网站开发游戏pragma.go 文件 文件位于: https://github.com/protocolbuffers/protobuf-go/blob/master/internal/pragma/pragma.go 该文件核心思想: 利用 Golang 语法机制,扩展 Golang 语言特性 目前,该文件提供以下 4 个功能: …

pragma.go 文件

文件位于: https://github.com/protocolbuffers/protobuf-go/blob/master/internal/pragma/pragma.go

该文件核心思想: 利用 Golang 语法机制,扩展 Golang 语言特性

目前,该文件提供以下 4 个功能:

功能说明
NoUnkeyedLiterals禁止非 {Key: Value} 方式,初始化结构体
DoNotImplement禁止第三方扩展实现其接口
DoNotCompare禁止结构体做比较运算
DoNotCopy禁止结构体值拷贝

下面依次说明实现原理

NoUnkeyedLiterals

定义如下:

// NoUnkeyedLiterals can be embedded in a struct to prevent unkeyed literals.
type NoUnkeyedLiterals struct{}

使用例子如下(原理见注释):

package mainimport ("fmt""unsafe"
)type NoUnkeyedLiterals struct{}type A1 struct {_  NoUnkeyedLiterals // 不影响 A1 结构体大小F1 intF2 string
}type A2 struct {F1 intF2 string
}func TestNoUnkeyedLiterals() {_ = A1{F1: 1, F2: "2"}_ = A2{1, "2"}                                          // 如果 A2 增加字段,可能会导致这里的初始化列表错误,而不知_ = A1{1, "2"}                                          // error: too few values in struct literal of type A1compilerInvalidStructLitfmt.Println(unsafe.Sizeof(A1{}) == unsafe.Sizeof(A2{})) // true
}func main() {TestNoUnkeyedLiterals()
}

DoNotImplement

定义如下:

// DoNotImplement can be embedded in an interface to prevent trivial
// implementations of the interface.
//
// This is useful to prevent unauthorized implementations of an interface
// so that it can be extended in the future for any protobuf language changes.
type DoNotImplement interface{ ProtoInternal(DoNotImplement) }

这个接口定义要结合internal目录,达成DoNotImplement接口对外部包不可见

比如protobuf-go中的FileImports接口,除了protobuf-go自己,其他第 3 方是没办法实现FileImports接口的

看下面代码:

import "google.golang.org/protobuf/internal/pragma"type doNotImplement pragma.DoNotImplement// FileImports is a list of file imports.
type FileImports interface {// Len reports the number of files imported by this proto file.Len() int// Get returns the ith FileImport. It panics if out of bounds.Get(i int) FileImportdoNotImplement
}

因为DoNotImplement 定义在internal目录下,对第 3 方不可见

FileImports又需要实现ProtoInternal(DoNotImplement)方法

因为第 3 方没有办法实现ProtoInternal(DoNotImplement),进而阻止了FileImports接口的第 3 方扩展实现

DoNotCompare

定义如下:

// DoNotCompare can be embedded in a struct to prevent comparability.
type DoNotCompare [0]func()
  • 用法和NoUnkeyedLiterals一样,内嵌到结构体内
  • 同样不占空间,不影响结构体大小(因为定义的是 0 长度数组)
  • 因为 func 类型不能比较大小,因此被内嵌的结构体也无法比较大小

例子略

DoNotCopy

定义如下:

// DoNotCopy can be embedded in a struct to help prevent shallow copies.
// This does not rely on a Go language feature, but rather a special case
// within the vet checker.
//
// See https://golang.org/issues/8005.
type DoNotCopy [0]sync.Mutex

例子如下:

package mainimport ("fmt""sync""unsafe"
)type DoNotCopy [0]sync.Mutextype A1 struct {_  DoNotCopy // 不影响 A1 结构体大小F1 intF2 string
}type A2 struct {F1 intF2 string
}func TestDoNotCopy(a A1) {  // line 22fmt.Println(unsafe.Sizeof(A1{}) == unsafe.Sizeof(A2{})) // true
}var a = A1{}func main() {TestDoNotCopy(a)  // line 29
}

执行go vet,会有提示:

./main.go:22:22: TestDoNotCopy passes lock by value: pragma.A1 contains sync.Mutex
./main.go:29:16: call of TestDoNotCopy copies lock value: pragma.A1 contains sync.Mutex
http://www.yayakq.cn/news/188932/

相关文章:

  • 品牌网站建设服务富蕴县建设局网站
  • 宁波做亚马逊网站wordpress是什么系统
  • 做汽车网站专业建设模式
  • 沈阳建立网站直播带货平台
  • 网站开发语言为wapwordpress页面定制
  • 建网站需要什么服务器重庆市建设工程信息网查证件
  • 云南建设厅网站监理员培训手把手教你实现电商网站开发
  • 惠州网站营销推广网站只收录无权重
  • 网站广告做的好的企业案例分析厦门谁需要网站建设
  • 食品企业网站模板南昌网站关键词排名
  • 网页设计个人网站作业深圳办公室租赁
  • 如何做网站使用手册紫色风格网站
  • 邢台网站建设报价多少钱域名备案要求
  • 成都家具企业网站建设wordpress首页设计
  • 网站关键词如何优化推广网站代码
  • 电商网站建设与管理实践网站建设优化推广教程
  • 玉树市wap网站建设公司构建企业门户网站的方法
  • 网站制作与网页建设找人做辅助的网站
  • 学网站开发需要报培训机构吗晋城手机网站建设
  • 自己做的网站网页滑动不莆田有哪几家做网站设计的
  • 胶州城阳网站建设jsp技术做网站有什么特点
  • 吉安市建设局网站html5网站开发框架
  • 专业做网站开发的公司网站建设的策划
  • 龙江网站设计制作洋气的文化传媒公司名字
  • 信用网站建设意义网站如何做长尾词排名
  • 国外网站服务器租用寮步营销型网站建设
  • 青岛专门做网站的公司商丘网站建设哪家专业
  • 检测一个网站用什么软件做的方法装修设计公司起名
  • 做一个网站的计划书wordpress 加载次序
  • 建网站个人主机做服务器网站开发技术期末考试试题