W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請(qǐng)參考代碼文檔:https://pkg.go.dev/github.com/gogf/gf/v2/os/gres
Add
?將?content
?解壓并添加到默認(rèn)資源對(duì)象。?prefix
?是非必要參數(shù),表示存儲(chǔ)到當(dāng)前資源對(duì)象中的每個(gè)文件的前綴。 func Add(content string, prefix ...string) error
package main
import "github.com/gogf/gf/v2/os/gres"
func main() {
//content內(nèi)容過(guò)長(zhǎng)已省略
if err := gres.Add("......"); err != nil {
panic("add binary content to resource manager failed: " + err.Error())
}
}
Load
?加載、解壓并將路徑為?path
?的文件數(shù)據(jù)讀取到默認(rèn)資源對(duì)象中。?prefix
?是非必要參數(shù),表示存儲(chǔ)到當(dāng)前資源對(duì)象中的每個(gè)文件的前綴。 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
?返回指定路徑的文件。
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
?用給定路徑?path
?搜索文件,如果文件是目錄,那么它會(huì)在這個(gè)目錄下進(jìn)行索引文件搜索。 ?GetWithIndex
?通常用于?http
?靜態(tài)文件服務(wù)。
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
?在默認(rèn)資源對(duì)象中直接返回路徑為?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
?檢查路徑為?path
?的資源是否存在于默認(rèn)資源對(duì)象中。
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
?檢查并返回資源管理器是否為空。
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
?返回給定路徑下的文件,參數(shù)?path
?應(yīng)該是文件夾類型。參數(shù)?pattern
?支持多個(gè)文件名模式,使用?,
?符號(hào)分隔多個(gè)模式。如果參數(shù)?recursive
?為?true
?,它會(huì)遞歸地掃描目錄。
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
?返回所有具有給定?path
?的絕對(duì)路徑的子文件,如果參數(shù)?recursive
?為?true
?,則會(huì)遞歸掃描目錄。
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
?將指定路徑?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
?打印默認(rèn)資源對(duì)象的文件。
func Dump()
package main
import (
"github.com/gogf/gf/v2/os/gres"
)
func main() {
gres.Add("xxxxxxxxx")
gres.Dump()
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: