十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.GO.dev/github.com/gogf/gf/v2/os/gres

Add将content解压并添加到默认资源对象。prefix是非必要参数,表示存储到当前资源对象中的每个文件的前缀。  func Add(content string, prefix ...string) errorpackage main
import "github.com/gogf/gf/v2/os/gres"
func main() {
        //content内容过长已省略
	if err := gres.Add("......"); err != nil {
		panic("add binary content to resource manager failed: " + err.Error())
	}
}Load加载、解压并将路径为path的文件数据读取到默认资源对象中。prefix是非必要参数,表示存储到当前资源对象中的每个文件的前缀。  func Load(path string, prefix ...string) errorpackage main
import "github.com/gogf/gf/v2/os/gres"
func main() {
	if err := gres.Load("../res/myfile"); err != nil {
		panic("load binary content to resource manager failed: " + err.Error())
	}
}Get返回指定路径的文件。 func Get(path string) *Filepackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	file := gres.Get("../res/myfile")
	if file == nil {
		glog.Error(gctx.New(), "get file failed!")
		return
	}
	fmt.Println("Get File Name:", file.Name())
}GetWithIndex用给定路径path搜索文件,如果文件是目录,那么它会在这个目录下进行索引文件搜索。 GetWithIndex通常用于http静态文件服务。   func GetWithIndex(path string, indexFiles []string) *Filepackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	file := gres.GetWithIndex("../res", []string{"myfile", "myconfig"})
	if file == nil {
		glog.Error(gctx.New(), "get file failed!")
		return
	}
	fmt.Println("Get File Name:", file.Name())
}GetContent在默认资源对象中直接返回路径为path的内容。   func GetContent(path string) []bytepackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	fileContent := gres.GetContent("../res/myfile")
	fmt.Println("Get File Content:", fileContent)
}Contains检查路径为path的资源是否存在于默认资源对象中。   func Contains(path string) boolpackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	if gres.Contains("../res/myfile") {
		fmt.Println("myfile is exist!")
	} else{
		fmt.Println("myfile is not exist!")
	}
}IsEmpty检查并返回资源管理器是否为空。  func IsEmpty() boolpackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	fmt.Println(gres.IsEmpty())
	gres.Add("xxxxxxxxxxxxxxxxx")
	fmt.Println(gres.IsEmpty())
	// Output:
	// true
	// false
}ScanDir返回给定路径下的文件,参数path应该是文件夹类型。参数pattern支持多个文件名模式,使用,符号分隔多个模式。如果参数recursive为true,它会递归地扫描目录。   func ScanDir(path string, pattern string, recursive ...bool) []*Filepackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	files := gres.ScanDir("../res", "*.doc,*.go", true)
	if len(files) > 0 {
		for _, file := range files {
			fmt.Println("ScanDir Result:", file.Name())
		}
	}
}ScanDirFile返回所有具有给定path的绝对路径的子文件,如果参数recursive为true,则会递归扫描目录。   func ScanDirFile(path string, pattern string, recursive ...bool) []*Filepackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	files := gres.ScanDirFile("../res", "*.*", true)
	if len(files) > 0 {
		for _, file := range files {
			fmt.Println("ScanDirFile Result:", file.Name())
		}
	}
}Export将指定路径src及其所有子文件递归保存到指定的系统路径dst。   func Export(src, dst string, option ...ExportOption) errorpackage main
import (
	"fmt"
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	err := gres.Export("../res/src", "../res/dst")
	if err != nil {
		fmt.Println("gres.Export Error:", err)
	}
}Dump打印默认资源对象的文件。   func Dump()package main
import (
	"github.com/gogf/gf/v2/os/gres"
)
func main() {
	gres.Add("xxxxxxxxx")
	gres.Dump()
}