tip
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 addscontent
to the default resource object.prefix
is an optional parameter that indicates the prefix for each file stored in the current resource object.Format:
func Add(content string, prefix ...string) error
- Example:
package main
import "github.com/gogf/gf/v2/os/gres"
func main() {
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+DwkxJCcWDN2IknmvjBEBoro/g0UJw2rLTQ17s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tiPLWK2fSvWSHSbuSedlgifhWqgA/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 {
panic("add binary content to resource manager failed: " + err.Error())
}
}
Load
Description:
Load
loads, decompresses, and reads file data at pathpath
into the default resource object.prefix
is an optional parameter that signifies the prefix for each file stored in the current resource object.Format:
func Load(path string, prefix ...string) error
- Example:
package 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
Description:
Get
returns the file at the specified path.Format:
func Get(path string) *File
- Example:
package 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
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:
func GetWithIndex(path string, indexFiles []string) *File
- Example:
package 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
Description:
GetContent
directly returns the content at pathpath
from the default resource object.Format:
func GetContent(path string) []byte
- Example:
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gres"
)
func main() {
fileContent := gres.GetContent("../res/myfile")
fmt.Println("Get File Content:", fileContent)
}
Contains
Description:
Contains
checks if a resource with the pathpath
exists in the default resource object.Format:
func Contains(path string) bool
- Example:
package 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
Description:
IsEmpty
checks and returns whether the resource manager is empty.Format:
func IsEmpty() bool
- Example:
package 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
Description:
ScanDir
returns files under a given path. The parameterpath
should be of type folder. The parameterpattern
supports multiple filename patterns, separated by,
. If the parameterrecursive
istrue
, it recursively scans directories.Format:
func ScanDir(path string, pattern string, recursive ...bool) []*File
- Example:
package 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
Description:
ScanDirFile
returns all subfiles for the given absolute pathpath
. If the parameterrecursive
istrue
, it will recursively scan directories.Note: Only returns files, not directories.
Format:
func ScanDirFile(path string, pattern string, recursive ...bool) []*File
- Example:
package 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
Description:
Export
recursively saves the specified pathsrc
and all its subfiles to the specified system pathdst
.Format:
func Export(src, dst string, option ...ExportOption) error
- Example:
package 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
Description:
Dump
prints the files of the default resource object.Format:
func Dump()
- Example:
package main
import (
"github.com/gogf/gf/v2/os/gres"
)
func main() {
gres.Add("xxxxxxxxx")
gres.Dump()
}