新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame資源管理-方法介紹
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請參考代碼文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/os/gres

Add
- 說明:?
Add?將?content?解壓并添加到默認資源對象。?prefix?是非必要參數(shù),表示存儲到當前資源對象中的每個文件的前綴。 - 格式:
func Add(content string, prefix ...string) error
- 示例:
package main
import "github.com/gogf/gf/v2/os/gres"
func main() {
//content內(nèi)容過長已省略
if err := gres.Add("......"); err != nil {
panic("add binary content to resource manager failed: " + err.Error())
}
}
Load
- 說明:?
Load?加載、解壓并將路徑為?path?的文件數(shù)據(jù)讀取到默認資源對象中。?prefix?是非必要參數(shù),表示存儲到當前資源對象中的每個文件的前綴。 - 格式:
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?靜態(tài)文件服務。 - 格式:
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?的內(nèi)容。 - 格式:
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?返回給定路徑下的文件,參數(shù)?path?應該是文件夾類型。參數(shù)?pattern?支持多個文件名模式,使用?,?符號分隔多個模式。如果參數(shù)?recursive?為?true?,它會遞歸地掃描目錄。 - 格式:
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?的絕對路徑的子文件,如果參數(shù)?recursive?為?true?,則會遞歸掃描目錄。 - 注意:只返回文件,不返回目錄。
- 格式:
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?及其所有子文件遞歸保存到指定的系統(tǒng)路徑?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()
} 網(wǎng)站標題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame資源管理-方法介紹
轉(zhuǎn)載源于:http://www.5511xx.com/article/djodicc.html


咨詢
建站咨詢
