以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.go.dev/github.com/gogf/gf/v2/os/gres

Add

  • 说明:Addcontent解压并添加到默认资源对象。prefix是非必要参数,表示存储到当前资源对象中的每个文件的前缀。

  • 格式:

    1. func Add(content string, prefix ...string) error
  • 示例:

    1. package main
    2. import "github.com/gogf/gf/v2/os/gres"
    3. func main() {
    4. if err := gres.Add("H4sIAAAAAAAC/5TWV1TTydsH8B+EpoAginEBDVIEQYp0kUTpSjAJhCwsxSwlgEtRwVCkKwJSo9IWCCChSAkoTZoivfeF0JEEQpMgvQrvcc/x1X/Zi/9zMTfzzPfMc/OZQcFBLKcBDoAD6DTsRQE/FS9wDLC/64xzl3PHuXng3GRt3R5iTFgBJt6X83cexsJLqjT4mrd9w0fQ6OPh+uAVEbGTj1k4TK1bdBu6nrT2Sab5GyC7ao5yc613l3Ly29Yfbl6fSovmph6AbK6L+TBrhl03yQhO9w7hHUcIcWSAEJd/z6I0MtJHaaKscBAb6STB5unZoVdhgDIFqQaCzyEuLwhza87xLoKCvMJtmjmYMYn63Sjx/X3IuE66VuXJKcVB08OurMS3IObBV6ilar8HjNhjTAGCzixCKecGJI4HfU3NBhngff1mmvfuihCWagSzqpwiiWaLhGkW8ZUMtrUOV0nZWSiGUl8ADw2xVAvrVMqUy2Fdmmmo4nl5CLKwynbmI/hlC5qZSd5INHqjEshnzRH8Sy60vatCdrS2MpPTWqGMEQEfxExDuC0T6/aRAmQZc8NLZ/0reVtVbeKKswKpFFBw4NFq4J7WWY2yE9p6i/oLwNCCT2HkioAirGf/cEcnDGH+6SCBPXAaCsiyOHNOMrfy8MpzqcyqbmcIUh9NqM3d240wHkieqIsLVQvYCCrFHtxYwQsjoEQNW6VrAbc/d9estPr1Cspt8kdMzResBrnITixfkXV+dH6IvwaET9i1fEFbKv2SHOHvYj8S86uPXU3zWv6LnglKVf37PpeU0WIVfU/lJFEJS++4ZAPt2x7NU1Ci+DwkxJCcWDN2IknmvjB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uXbbzHuqjLmpdmuad6MtiPLWK2fSvWSHSbuSedlgifhWqgA/FebVe9NopPn5Dd9xRPFBrlB5KL5q+SbnDSP0gQHe7r5+o2VHNaYUIaRHkWygeRyC72aU5sT6mRuUR0W/4eBw0ZQvzfDpz1/TuZ7T4zfLpSJfOz6GbMkrTitSSv4ws1kn55L1h4RyMbcUaB4p/VVU+4vmOKYfAsggbKwBEsP3AFACgAWFGP2PK+h3TvwUNyejBfjv7cwcKzsR8GvSD4p+R/kbx98oM+rb+4+ft32N+vt6/xoixAP/s+4+Y/z7L9yfiSPPBMeA/JmNl+7YLAkBAOwAA5L/b/y8AAP//A6tAlY0KAAA="); err != nil {
    5. panic("add binary content to resource manager failed: " + err.Error())
    6. }
    7. }

Load

  • 说明:Load加载、解压并将路径为path的文件数据读取到默认资源对象中。prefix是非必要参数,表示存储到当前资源对象中的每个文件的前缀。

  • 格式:

    1. func Load(path string, prefix ...string) error
  • 示例:

    1. package main
    2. import "github.com/gogf/gf/v2/os/gres"
    3. func main() {
    4. if err := gres.Load("../res/myfile"); err != nil {
    5. panic("load binary content to resource manager failed: " + err.Error())
    6. }
    7. }

Get

  • 说明:Get返回指定路径的文件。

  • 格式:

    1. func Get(path string) *File
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gctx"
    5. "github.com/gogf/gf/v2/os/glog"
    6. "github.com/gogf/gf/v2/os/gres"
    7. )
    8. func main() {
    9. file := gres.Get("../res/myfile")
    10. if file == nil {
    11. glog.Error(gctx.New(), "get file failed!")
    12. return
    13. }
    14. fmt.Println("Get File Name:", file.Name())
    15. }

GetWithIndex

  • 说明:GetWithIndex用给定路径path搜索文件,如果文件是目录,那么它会在这个目录下进行索引文件搜索。 GetWithIndex通常用于http静态文件服务。

  • 格式:

    1. func GetWithIndex(path string, indexFiles []string) *File
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gctx"
    5. "github.com/gogf/gf/v2/os/glog"
    6. "github.com/gogf/gf/v2/os/gres"
    7. )
    8. func main() {
    9. file := gres.GetWithIndex("../res", []string{"myfile", "myconfig"})
    10. if file == nil {
    11. glog.Error(gctx.New(), "get file failed!")
    12. return
    13. }
    14. fmt.Println("Get File Name:", file.Name())
    15. }

GetContent

  • 说明:GetContent在默认资源对象中直接返回路径为path的内容。

  • 格式:

    1. func GetContent(path string) []byte
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. fileContent := gres.GetContent("../res/myfile")
    8. fmt.Println("Get File Content:", fileContent)
    9. }

Contains

  • 说明:Contains检查路径为path的资源是否存在于默认资源对象中。

  • 格式:

    1. func Contains(path string) bool
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. if gres.Contains("../res/myfile") {
    8. fmt.Println("myfile is exist!")
    9. } else{
    10. fmt.Println("myfile is not exist!")
    11. }
    12. }

IsEmpty

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

  • 格式:

    1. func IsEmpty() bool
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. fmt.Println(gres.IsEmpty())
    8. gres.Add("xxxxxxxxxxxxxxxxx")
    9. fmt.Println(gres.IsEmpty())
    10. // Output:
    11. // true
    12. // false
    13. }

ScanDir

  • 说明:ScanDir返回给定路径下的文件,参数path应该是文件夹类型。参数pattern支持多个文件名模式,使用符号分隔多个模式。如果参数recursivetrue,它会递归地扫描目录。

  • 格式:

    1. func ScanDir(path string, pattern string, recursive ...bool) []*File
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. files := gres.ScanDir("../res", "*.doc,*.go", true)
    8. if len(files) > 0 {
    9. for _, file := range files {
    10. fmt.Println("ScanDir Result:", file.Name())
    11. }
    12. }
    13. }

ScanDirFile

  • 说明:ScanDirFile返回所有具有给定path的绝对路径的子文件,如果参数recursivetrue,则会递归扫描目录。

  • 注意:只返回文件,不返回目录。

  • 格式:

    1. func ScanDirFile(path string, pattern string, recursive ...bool) []*File
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. files := gres.ScanDirFile("../res", "*.*", true)
    8. if len(files) > 0 {
    9. for _, file := range files {
    10. fmt.Println("ScanDirFile Result:", file.Name())
    11. }
    12. }
    13. }

Export

  • 说明:Export将指定路径src及其所有子文件递归保存到指定的系统路径dst

  • 格式:

    1. func Export(src, dst string, option ...ExportOption) error
  • 示例:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/v2/os/gres"
    5. )
    6. func main() {
    7. err := gres.Export("../res/src", "../res/dst")
    8. if err != nil {
    9. fmt.Println("gres.Export Error:", err)
    10. }
    11. }

Dump

  • 说明:Dump打印默认资源对象的文件。

  • 格式:

    1. func Dump()
  • 示例:

    1. package main
    2. import (
    3. "github.com/gogf/gf/v2/os/gres"
    4. )
    5. func main() {
    6. gres.Add("xxxxxxxxx")
    7. gres.Dump()
    8. }