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

go语言内置预编译 //go:embed xxx 使用详解

在go语言里面,我们可以使用一个“类注释”的语法来来让编译器帮助我们在编译的时候将一些文件或者目录读取到指定的变量中来供我们使用。

go:embed语法: 

//go:embed 文件或者目录路径

var 变量名 变量类型

说明:

文件或者目录路径 可以是相对路径,也可以是绝对路径,路径中可以使用通配符*来指定要加载的文件类型,这个的用法和 filepath.Glob(pattern string)函数的用法是一样的.

变量类型 这里只支持2种变量类型 string 或者 embed.FS , 这个embed.FS是一个结构体,专门用来接收文件集合的,注意是只读文件集合

使用示例

在下面的示例中我们定义了2个全局变量:  MyAbc用来接收abc.txt中的内容;  MyStaticFs用来接收statics文件夹下的html文件信息。 在 fs_test.go文件中,我们演示了如何使用我们定义的预编译变量,和如何将 embed.FS类型转换为 http.FileSystem 以及创建一个简单的静态服务示例。

假设我们的文件目录结构如下

├── abc.txt
├── fs.go
├── main.go
└── statics└── index.html

abc.txt 的文件内容

abc123

fs.go 这个是我们的//go:embed的预编译定义

package mainimport ("embed"
)//go:embed abc.txt
var MyAbc string//go:embed statics/*.html
var MyStaticFs embed.FS

fs_test.go使用示例

package mainimport ("fmt""net/http""testing"
)func TestDemo(t *testing.T) {abc := MyAbc// 使用预编译的变量fmt.Println("预编译变量MyAbc的内容为:", abc) // abc123// 这里我们就可以直接使用我们定义的预编译变量了, 他的类型是 embed.FSstatics := MyStaticFs// 创建一个静态文件服务的handler  注意这里使用的是FileServerFS// handler := http.FileServerFS(statics)// 如果要是哟共 FileServer 则需要将类型embed.FS转换为http.FileSystemstaticsFs := http.FS(statics)handler := http.FileServer(staticsFs)http.ListenAndServe(":8000", handler)
}

运行内存图解和总结

通过上面的图示,我们可以看到,编译器将文件abc.txt的内容读取并赋值给了我们定义的变量MyAbc,  将文件夹 statics 中的html文件和文件夹自己放入到了我们定义的 embed.FS 类型变量 MyStaticFs里面, 在这个变量里面包含了我们定义的文件的名称完整内容和文件hash等信息,可见go是吧我们指定的文件夹下面的所有文件内容都读取到了FS变量里面了,所以这个地方建议只放小文件,大文件千万别用这种模式来操作!!!

embed.FS只读文件集合结构体定义参考:

这个里面详情阐述了FS结构体的用法和 文件模式的用法。


// An FS is a read-only collection of files, usually initialized with a //go:embed directive.
// When declared without a //go:embed directive, an FS is an empty file system.
//
// An FS is a read-only value, so it is safe to use from multiple goroutines
// simultaneously and also safe to assign values of type FS to each other.
//
// FS implements fs.FS, so it can be used with any package that understands
// file system interfaces, including net/http, text/template, and html/template.
//
// See the package documentation for more details about initializing an FS.
type FS struct {// The compiler knows the layout of this struct.// See cmd/compile/internal/staticdata's WriteEmbed.//// The files list is sorted by name but not by simple string comparison.// Instead, each file's name takes the form "dir/elem" or "dir/elem/".// The optional trailing slash indicates that the file is itself a directory.// The files list is sorted first by dir (if dir is missing, it is taken to be ".")// and then by base, so this list of files:////	p//	q///	q/r//	q/s///	q/s/t//	q/s/u//	q/v//	w//// is actually sorted as:////	p       # dir=.    elem=p//	q/      # dir=.    elem=q//	w/      # dir=.    elem=w//	q/r     # dir=q    elem=r//	q/s/    # dir=q    elem=s//	q/v     # dir=q    elem=v//	q/s/t   # dir=q/s  elem=t//	q/s/u   # dir=q/s  elem=u//// This order brings directory contents together in contiguous sections// of the list, allowing a directory read to use binary search to find// the relevant sequence of entries.files *[]file
}

http://www.lryc.cn/news/366498.html

相关文章:

  • 数据挖掘--挖掘频繁模式、关联和相关性:基本概念和方法
  • Locust:用Python编写可扩展的负载测试
  • 【Neo4j】Windows11使用Neo4j导入CSV数据可视化知识图谱
  • 探索智慧林业系统的总体架构与应用
  • 【JSP】如何在IDEA上部署JSP WEB开发项目
  • 用HTML实现拓扑面,动态4D圆环面,可手动调节,富有创新性的案例。(有源代码)
  • java调用GDAL及JTS实现生成泰森多边形(Voronoi图)的一种方法
  • Type-C音频转接器方案
  • linux 服务器上离线安装 node nvm
  • Web前端三大主流框架:React、Angular和Vue的比较与选择
  • C# MemoryCache 缓存应用
  • 【学习笔记】Linux前置准备
  • 各种空气能热泵安装图
  • 软件杯 题目:基于深度学习的中文对话问答机器人
  • UI学习笔记(一)
  • 【C语言训练题库】扫雷->简单小游戏!
  • WMS仓储管理系统高效驱动制造企业物料管理
  • python使用appium打开程序后,为什么没有操作后程序就自动退出了
  • MacBook M系列芯片安装php8.2
  • OlSoul系统调校程序v2024.06.05
  • 图像特征提取 python
  • width: 100%和 width: 100vw这两种写法有什么区别
  • 如何在另一台电脑上使用相同的Python环境和依赖包
  • Vue3 响应式 API:工具函数(一)
  • 开发常用软件
  • conntrack如何限制您的k8s网关
  • SwiftUI六组合复杂用户界面
  • 高考分数查询结果自动推送至微信
  • flask_sqlalchemy时间缓存导致datetime.now()时间不变问题
  • 使用 PAI-DSW x Free Prompt Editing图像编辑算法,开发个人AIGC绘图小助理