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

protobuf-go pragma.go 文件介绍

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.lryc.cn/news/290481.html

相关文章:

  • C#设置程序开机启动
  • 爱可声助听器参与南湖区价值百万公益助残捐赠活动成功举行
  • SpringBoot 实现定时任务
  • 将Vue2中的console.log调试信息移除
  • EMC设计检查建议,让PCB layout达到最佳性能
  • 常用抓包软件集合(Fiddler、Charles)
  • C++入门(一)— 使用VScode开发简介
  • PeakCAN连接到WSL2 Debian
  • Spring Boot导出EXCEL 文件
  • 编程笔记 html5cssjs 060 css响应式布局
  • 建筑行业如何应用3D开发工具HOOPS提升实时设计体验?
  • 【grafana】使用教程
  • seata 分布式
  • 前端面试题-说说你了解的js数据结构?(2024.1.29)
  • 音视频数字化(数字与模拟-录音机)
  • 鸿蒙开发-UI-组件3
  • 安全测试几种:代码静态扫描、模糊测试、黑盒测试、白盒测试、渗透测试
  • Mac安装及配置MySql及图形化工具MySQLworkbench安装
  • 【Vue】为什么Vue3使用Proxy代替defineProperty?
  • 3、css设置样式总结、节点、节点之间关系、创建元素的方式、BOM
  • 计算机网络-物理层传输介质(导向传输介质-双绞线 同轴电缆 光纤和非导向性传输介质-无线波 微波 红外线 激光)
  • springboot3+vue3支付宝在线支付案例-渲染产品列表页面
  • 数字美妆技术:美颜SDK和动态贴纸技术的崭新时代
  • 使用OpenCV实现一个简单的实时人脸跟踪
  • 关于监控的那些事,你有必要了解一下
  • C#学习笔记_数组
  • 微信小程序canvas画布实现文字自由缩放、移动功能
  • jQuery 获取并设置 CSS 类 —— W3school 详解 简单易懂(十五)
  • dart使用教程
  • CSS3:最新特性和实例教程