Resource - Methods - 图1tip

The following is a list of common methods. Documentation updates may lag behind new code features; please refer to the code documentation for more methods and examples: https://pkg.go.dev/github.com/gogf/gf/v2/os/gres

Add

  • Description: Add decompresses and adds content to the default resource object. prefix is an optional parameter that indicates the prefix for each file stored in the current resource object.

  • Format:

  1. func Add(content string, prefix ...string) error
  • Example:
  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+DwkxJCcW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cncNr++XOPVuXbbzHuqjLmpdmuad6MtiPLWK2fSvWSHSbuSedlgifhWqgA/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

  • Description: Load loads, decompresses, and reads file data at path path into the default resource object. prefix is an optional parameter that signifies the prefix for each file stored in the current resource object.

  • Format:

  1. func Load(path string, prefix ...string) error
  • Example:
  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

  • Description: Get returns the file at the specified path.

  • Format:

  1. func Get(path string) *File
  • Example:
  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

  • Description: GetWithIndex searches for a file with the given path. If the file is a directory, it searches for index files within that directory. GetWithIndex is typically used for HTTP static file services.

  • Format:

  1. func GetWithIndex(path string, indexFiles []string) *File
  • Example:
  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

  • Description: GetContent directly returns the content at path path from the default resource object.

  • Format:

  1. func GetContent(path string) []byte
  • Example:
  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

  • Description: Contains checks if a resource with the path path exists in the default resource object.

  • Format:

  1. func Contains(path string) bool
  • Example:
  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

  • Description: IsEmpty checks and returns whether the resource manager is empty.

  • Format:

  1. func IsEmpty() bool
  • Example:
  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

  • Description: ScanDir returns files under a given path. The parameter path should be of type folder. The parameter pattern supports multiple filename patterns, separated by ,. If the parameter recursive is true, it recursively scans directories.

  • Format:

  1. func ScanDir(path string, pattern string, recursive ...bool) []*File
  • Example:
  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

  • Description: ScanDirFile returns all subfiles for the given absolute path path. If the parameter recursive is true, it will recursively scan directories.

  • Note: Only returns files, not directories.

  • Format:

  1. func ScanDirFile(path string, pattern string, recursive ...bool) []*File
  • Example:
  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

  • Description: Export recursively saves the specified path src and all its subfiles to the specified system path dst.

  • Format:

  1. func Export(src, dst string, option ...ExportOption) error
  • Example:
  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

  • Description: Dump prints the files of the default resource object.

  • Format:

  1. func Dump()
  • Example:
  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. }