资源类

package
v0.0.0-...-2910145 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package gres 提供了资源管理功能以及文件和字节之间的打包/解包功能。

Index

Constants

View Source
const (
	// DefaultName 默认的实例使用组名。
	DefaultName = "default"
)
View Source
const (
	// 目录分隔符。
	Separator = "/"
)

Variables

This section is empty.

Functions

func Add

func Add(content string, prefix ...string) error

Add 函数将解包并把 `content` 添加到默认资源对象中。 不必要的参数 `prefix` 表示存储到当前资源对象中每个文件的前缀。

func Contains

func Contains(path string) bool

Contains 检查默认资源对象中是否存在 `path`。

func Dump

func Dump()

Dump 打印默认资源对象中的文件。

func Export

func Export(src, dst string, option ...ExportOption) error

Export 函数会递归地导出并保存指定路径 `src` 及其所有子文件到指定的系统路径 `dst`。

func GetContent

func GetContent(path string) []byte

GetContent 直接返回默认资源对象中 `path` 的内容。

func IsEmpty

func IsEmpty() bool

IsEmpty 检查并返回资源管理器是否为空。

func Load

func Load(path string, prefix ...string) error

Load 从`path`加载、解压并将数据添加到默认资源对象中。 不必要的参数`prefix`表示存储到当前资源对象时每个文件的前缀。

func Pack

func Pack(srcPaths string, keyPrefix ...string) ([]byte, error)

Pack 将由 `srcPaths` 指定的路径打包成字节形式。 不必要的参数 `keyPrefix` 表示每个文件被打包到结果字节中时的前缀。

注意,参数 `srcPaths` 支持使用 ',' 连接的多个路径。

已弃用:请改用 PackWithOption。

func PackToFile

func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error

PackToFile 将由`srcPaths`指定的路径打包到目标文件`dstPath`中。 不必要的参数`keyPrefix`表示每个被打包到结果字节中的文件前缀。

注意,参数`srcPaths`支持使用','连接的多个路径。

已弃用:请改用PackToFileWithOption。

func PackToFileWithOption

func PackToFileWithOption(srcPaths, dstPath string, option Option) error

PackToFileWithOption 将由 `srcPaths` 指定的路径打包到目标文件 `dstPath`。

注意,参数 `srcPaths` 支持通过 ',' 连接的多个路径。

func PackToGoFile

func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error

PackToGoFile 将由 `srcPaths` 指定的路径打包到目标 Go 文件 `goFilePath`, 并使用给定的包名 `pkgName`。

非必需参数 `keyPrefix` 表示每个打包进结果字节流文件的前缀。

注意,参数 `srcPaths` 支持以 ',' 连接的多个路径。

已弃用:请改用 PackToGoFileWithOption。

func PackToGoFileWithOption

func PackToGoFileWithOption(srcPath, goFilePath, pkgName string, option Option) error

PackToGoFileWithOption 根据指定的 `srcPaths` 路径将文件打包到目标 Go 文件 `goFilePath`, 同时使用给定的包名 `pkgName`。

注意,参数 `srcPaths` 支持通过 ',' 连接多个路径。

func PackWithOption

func PackWithOption(srcPaths string, option Option) ([]byte, error)

PackWithOption 函数将由 `srcPaths` 指定的路径打包成字节形式。

注意,参数 `srcPaths` 支持使用 ',' 连接的多个路径。

Types

type ExportOption

type ExportOption struct {
	RemovePrefix string // 从资源中移除文件名前缀
}

ExportOption 是函数 Export 的选项。

type File

type File struct {
	// contains filtered or unexported fields
}

func Get

func Get(path string) *File

Get 返回指定路径的文件。

func GetWithIndex

func GetWithIndex(path string, indexFiles []string) *File

GetWithIndex 搜索指定 `path` 的文件,如果该文件是一个目录, 则进一步在该目录下进行索引文件的搜索。

GetWithIndex 通常用于 HTTP 静态文件服务。

func ScanDir

func ScanDir(path string, pattern string, recursive ...bool) []*File

ScanDir 返回给定路径下的文件,参数 `path` 应为文件夹类型。

参数 `pattern` 支持多个文件名模式, 使用 ',' 符号分隔多个模式。

若给定的参数 `recursive` 为 true,则会递归扫描目录。

func ScanDirFile

func ScanDirFile(path string, pattern string, recursive ...bool) []*File

ScanDirFile 返回给定 `path` 下所有子文件的绝对路径, 若给定参数 `recursive` 为 true,则会递归扫描目录。

注意,该函数仅返回文件,不包括目录。

func Unpack

func Unpack(path string) ([]*File, error)

Unpack 将由 `path` 指定的内容解包为 []*File 类型的切片。

func UnpackContent

func UnpackContent(content string) ([]*File, error)

UnpackContent 解析内容到 []*File 类型的切片。

func (*File) Close

func (f *File) Close() error

Close 实现了 http.File 接口的方法。

func (*File) Content

func (f *File) Content() []byte

Content 返回文件的内容。

func (*File) Export

func (f *File) Export(dst string, option ...ExportOption) error

Export 递归地将所有子文件导出并保存到指定的系统路径 `dst`。

func (*File) FileInfo

func (f *File) FileInfo() os.FileInfo

FileInfo返回FileHeader对应的os.FileInfo对象。

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

func (*File) Name

func (f *File) Name() string

Name 返回文件的名称。

func (*File) Open

func (f *File) Open() (io.ReadCloser, error)

Open 函数返回一个 ReadCloser 类型的对象,该对象提供了对文件内容的访问权限。 允许同时读取多个文件。

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read 实现了 io.Reader 接口。

func (*File) Readdir

func (f *File) Readdir(count int) ([]os.FileInfo, error)

Readdir 实现了 http.File 接口中的 Readdir 方法。

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (n int64, err error)

Seek 实现了 io.Seeker 接口。

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat 实现了 http.File 接口的 Stat 方法。

type Option

type Option struct {
	Prefix   string // 在资源管理器中,每个文件项的文件路径前缀。
	KeepPath bool   // 在打包时保留传递的路径,通常用于相对路径。
}

Option 包含 Pack 函数的额外选项。

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

func Instance

func Instance(name ...string) *Resource

Instance 返回一个 Resource 的实例。 参数 `name` 是该实例的名称。

func New

func New() *Resource

New 创建并返回一个新的资源对象。

func (*Resource) Add

func (r *Resource) Add(content string, prefix ...string) error

Add 方法对`content`进行解包并将其添加到当前资源对象中。 不必要的参数`prefix`表示存储到当前资源对象时,每个文件的前缀。

func (*Resource) Contains

func (r *Resource) Contains(path string) bool

Contains 检查当前资源对象中是否存在 `path`。

func (*Resource) Dump

func (r *Resource) Dump()

Dump 打印当前资源对象的文件。

func (*Resource) Export

func (r *Resource) Export(src, dst string, option ...ExportOption) error

Export 递归地导出并保存指定路径`srcPath`及其所有子文件到指定系统路径`dstPath`。

func (*Resource) Get

func (r *Resource) Get(path string) *File

Get 返回指定路径的文件。

func (*Resource) GetContent

func (r *Resource) GetContent(path string) []byte

GetContent直接返回`path`的内容。

func (*Resource) GetWithIndex

func (r *Resource) GetWithIndex(path string, indexFiles []string) *File

GetWithIndex 搜索指定 `path` 的文件,如果该文件是一个目录, 则进一步在该目录下进行索引文件的搜索。

GetWithIndex 通常用于 HTTP 静态文件服务。

func (*Resource) IsEmpty

func (r *Resource) IsEmpty() bool

IsEmpty 检查并返回资源管理器是否为空。

func (*Resource) Load

func (r *Resource) Load(path string, prefix ...string) error

Load 从`path`加载、解压并将数据添加到当前资源对象中。 不必要的参数`prefix`表示存储到当前资源对象中的每个文件的前缀。

func (*Resource) ScanDir

func (r *Resource) ScanDir(path string, pattern string, recursive ...bool) []*File

ScanDir 返回给定路径下的文件,参数`path`应为文件夹类型。

参数`pattern`支持多个文件名模式, 使用','符号来分隔多个模式。

如果给定参数`recursive`为真,则会递归扫描目录。

注意,返回的文件列表中不包含给定的参数`path`所代表的目录自身。

func (*Resource) ScanDirFile

func (r *Resource) ScanDirFile(path string, pattern string, recursive ...bool) []*File

ScanDirFile 返回给定 `path` 下所有子文件的绝对路径, 若给定参数 `recursive` 为 true,则会递归扫描目录。

注意,该函数仅返回文件,不包括目录。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL