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

做ppt介绍网站吗外贸网站建设 google

做ppt介绍网站吗,外贸网站建设 google,做文章网站,音乐网站怎么做在go语言里面,我们可以使用一个“类注释”的语法来来让编译器帮助我们在编译的时候将一些文件或者目录读取到指定的变量中来供我们使用。 go:embed语法: //go:embed 文件或者目录路径 var 变量名 变量类型 说明: 文件或者目录路径 可以…

在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.tj-hxxt.cn/news/41822.html

相关文章:

  • 网站建设内容存储网上广告怎么推广
  • 网站开发与网页后台开发新网站排名优化怎么做
  • aspcms网站打不开想开个网站怎样开
  • 成都网站建设公司思乐科技公司营销策划方案
  • 丹东网站设计百度推广按点击收费
  • 政务公开网站建设管理怎么网站排名seo
  • 淮安网站优化中国新冠一共死去的人数
  • 聊城专业做网站做品牌推广应该怎么做
  • wordpress前台配色西安seo关键词查询
  • 用香港阿里云做网站好有钱互联网线上推广
  • 哪些网站可以接单做肇庆seo优化
  • wordpress新闻是哪个表站长工具seo查询软件
  • 专门做图片是网站网络营销策略优化
  • 哪个网站简历做的好各国足球世界排名
  • 承德百度网站建设短视频seo关键词
  • 商业招商网站广州百度推广代理公司
  • 网站开发app开发培训关键词分析软件
  • 用模板做网站教程百度网页版
  • 网站建设经典范例简述网络营销与传统营销的整合
  • 广东广东深圳网站建设域名ip查询入口
  • 品牌网站建设绿d茶建网站建设
  • 关键词推广公司windows优化大师官方网站
  • 做网站的企业关键词挖掘长尾词工具
  • 徐州教育学会网站建设百度快速收录教程
  • 怎样创建网站快捷方式河南今日头条新闻最新
  • 怎么做外贸网站推广网页制作在线生成
  • 珠海建站服务免费网站谁有靠谱的
  • b2c电子商务网站建设网站百度百科
  • 国内公司名字可以做国外网站windows优化大师电脑版
  • 广州牌具做网站的公司如何做好关键词的优化