跳到主要内容
版本:2.8.x(Latest)
提示

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

Add

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

  • 格式:

func Add(content string, prefix ...string) error
  • 示例:
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+Dwkx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BalpLcncNr++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 {
panic("add binary content to resource manager failed: " + err.Error())
}
}

Load

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

  • 格式:

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

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

  • 格式:

func Get(path string) *File
  • 示例:
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

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

  • 格式:

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

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

  • 格式:

func GetContent(path string) []byte
  • 示例:
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

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

  • 格式:

func Contains(path string) bool
  • 示例:
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

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

  • 格式:

func IsEmpty() bool
  • 示例:
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

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

  • 格式:

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

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

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

  • 格式:

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

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

  • 格式:

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

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

  • 格式:

func Dump()
  • 示例:
package main

import (
"github.com/gogf/gf/v2/os/gres"
)

func main() {
gres.Add("xxxxxxxxx")

gres.Dump()
}