GoFrame 资源管理-方法介绍

2022-04-07 10:45 更新

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

Add

  • 说明:​Add​将​content​解压并添加到默认资源对象。​prefix​是非必要参数,表示存储到当前资源对象中的每个文件的前缀。  
  • 格式:
func Add(content string, prefix ...string) error
  • 示例:
package main

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

func main() {
        //content内容过长已省略
	if err := gres.Add("......"); 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​支持多个文件名模式,使用​​符号分隔多个模式。如果参数​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​的绝对路径的子文件,如果参数​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​及其所有子文件递归保存到指定的系统路径​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()
}


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号