13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联GoFrame教程:GoFrame资源管理-方法介绍

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

Add

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

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

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

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

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

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

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

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

func Dump()

package main

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

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

	gres.Dump()
}

当前标题:创新互联GoFrame教程:GoFrame资源管理-方法介绍
网页地址:http://cdbrznjsb.com/article/djodicc.html

其他资讯

让你的专属顾问为你服务