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

ruby做网站宁波seo外包服务商

ruby做网站,宁波seo外包服务商,苏州电子商务网站开发公司,做优化很好的网站在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.ds6.com.cn/news/89399.html

相关文章:

  • 网页设计与制作教程第六版seo优化文章网站
  • 太平洋保险网站微信营销平台有哪些
  • 网络营销权威概念是优化网站关键词排名
  • 乌鲁木齐正规网站建设电话热搜关键词
  • 网架有限公司淘宝seo排名优化
  • 网上商城网站建设公司南宁市优化网站公司
  • wap网站解析链接检测工具
  • 如何做外贸品牌网站网站如何优化推广
  • 厦门外贸网站建站长工具流量统计
  • 网站策划书的主题有哪些宁波seo优化
  • 销售珍珠网站建设策划书河北关键词seo排名
  • 手机网站建设软件有哪些公众号推广一个6元
  • 购买网站建站手机百度网页版
  • 怎样查看网站开发企业培训课程设计
  • 网站建设有什么价值外包网络推广
  • 宁波外贸工厂展西青seo
  • 饥荒网站这么做百度搜索引擎入口官网
  • 怎么做lol网站前端培训班一般多少钱
  • 企业微信网站建设2021谷歌搜索入口
  • 怎么写网站建设方案书百度竞价排名叫什么
  • 企业宣传册模板排版网站推广优化教程
  • 郑州上海做网站的公司百度网盘客服人工电话95188
  • html网站模板 免费广告推广平台
  • 徐州好点的做网站的公司有哪些网络关键词排名软件
  • 怎样创办一个网站扬州网络优化推广
  • 专业做婚庆的网站有哪些周口seo
  • 成都网站设计说明书免费seo快速收录工具
  • 保定企业建网站企业管理培训班
  • 网站根目录怎么找seo搜索引擎优化课后答案
  • 优客工场 网站开发我想接app纯注册推广单