Introduction
Resource Management
refers to the ability to package any file/directory into a Golang
source file and compile it into an executable file, which is then released with the executable file.
When the program starts, resource files will be decompressed and released into memory for read-only access by the program. You can consider it as a memory-based file manager. Additionally, the GoFrame
resource management feature supports packaging files/directories for use as standalone binary resource files. Since resource file operations are memory-based during program execution, there is no disk IO
overhead, resulting in very high file operation efficiency.
Usage:
import "github.com/gogf/gf/v2/os/gres"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/os/gres
Features
The gres
resource management component has the following notable features:
- It can package any file/directory as a
Go
file, supporting custom encryption and decryption. - The packaged
Go
file/resource file is automatically compressed, with commoncss/js
files achieving a compression rate of50~90%
. - It supports easily exporting resource content packaged in
Go
files to the local file system. - Resource manager content is entirely memory-based and read-only, with no dynamic modification possible.
- The resource manager is integrated by default with the
WebServer
, configuration management, and template engine modules. - Any file, such as website static files or configuration files, can be compiled into a binary file and also into the released executable file.
- Developers can publish just one executable file, making software distribution easier and protecting software intellectual property possible.
Comparison with embed.FS
Starting from Golang v1.16
, the official release provides a static file embedding feature embed.FS
. The overall underlying design is similar to the gres
component, with comparable compression rates and execution efficiency, although there are significant differences in usage design and engineering management. The GoFrame
resource management component is more feature-rich, and the core components of the framework have been fully integrated with the gres
resource management component. Under GoFrame's standard engineering management, developers can seamlessly use the resource management features without being aware of them. For details, see the chapter Resource - Best Practices.
In the future, the GoFrame base framework will not consider built-in support for the embed.FS
component. embed.FS
and the gres
component can be independently used without affecting each other.
Documents
📄️ Resource - Packing With Tool
Use the gf command line tool in the GoFrame framework for resource packaging. The gf pack command can package the project's config, public, and template directories into Go files, and the generated files are automatically introduced into the project. The packed resource package is preferentially introduced in the boot package, and the packaged resource files are operated through the gres module. The gres.Dump() method can print the resource file list for easy management and debugging.
📄️ Resource - Packing With Method
Resource management and method packing using the GoFrame framework. Implement custom methods for packaging and unpacking files and directories, supporting binary and Go code files. The examples also demonstrate how to protect resource file contents through custom encryption and decryption, providing detailed interface documentation and implementation details.
📄️ Resource - Methods
Methods for resource management in the GoFrame framework, including how to add resources, load resource files, get specified path files, check if resources exist, scan files in directories, and provide relevant example code for understanding and usage.
📄️ Resource - Examples
An example of resource management using the GoFrame framework, demonstrating its application in the static service of a WebServer, configuration management, and template engine. By importing resource files, convenient resource management is achieved without extra code setup.
📄️ Resource - Best Practices
Best practices for resource management in the GoFrame framework. By using the engineering directory structure and CLI tools provided by GoFrame, developers can effectively manage static resources without affecting the development process. During the release phase, the resource components package static files into the binary executable file, achieving efficient resource release and management.