GoFrame 資源管理-方法介紹

2022-04-07 10:45 更新

以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請(qǐng)參考代碼文檔:https://pkg.go.dev/github.com/gogf/gf/v2/os/gres

Add

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?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

  • 說(shuō)明:?Dump?打印默認(rèn)資源對(duì)象的文件。  
  • 格式:

func Dump()

  • 示例:

package main

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

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

	gres.Dump()
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)