Introduction
Static resource module, provides access to the embedded filesystem based on Go's embed.FS. Supports checking if files exist and reading JSON configuration files (strict mode and tolerant mode). Strict mode throws an exception and exits directly on file read failure or parse failure, while tolerant mode returns an error for business code to handle.
Import
import "github.com/wallace5303/ee-go/estatic"API
StaticFS
Description: Embedded filesystem instance, type embed.FS. Business code accesses compile-time embedded static resource files through this variable.
Type: embed.FS
Example:
data, err := estatic.StaticFS.ReadFile("config/app.json")
if err != nil {
fmt.Println("read failed:", err)
}FileIsExist(name)
Description: Check whether a specified file exists in the embedded filesystem. Determines file existence by calling StaticFS.ReadFile and checking whether it returns an error.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | File path in the embedded filesystem, e.g. "config/app.json" |
Returns: bool — true if file exists, false if it doesn't
Example:
if estatic.FileIsExist("public/html/failure.html") {
fmt.Println("file exists in embedded FS")
}ReadJsonStrict(f)
Description: Read a JSON file from the embedded filesystem and parse it into map[string]any. Strict mode: on read failure or JSON parse failure, calls eerror.ThrowWithCode to throw an exception and exit the process (exit code ExitConfigFileFS). Suitable for configuration files that must exist.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| f | string | Yes | JSON file path in the embedded filesystem |
Returns: map[string]any — key-value mapping after parsing the JSON file
Example:
config := estatic.ReadJsonStrict("config/app.json")
fmt.Println("app name:", config["name"])See also: eutil.ReadJsonStrict — Read JSON from disk filesystem (strict mode)
ReadJson(f)
Description: Read a JSON file from the embedded filesystem and parse it into map[string]any. Tolerant mode: on read failure or JSON parse failure, returns an error without exiting the process. Suitable for optional configuration files.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| f | string | Yes | JSON file path in the embedded filesystem |
Returns: (map[string]any, error) — on success returns JSON parse result and nil, on failure returns nil and error information
Example:
config, err := estatic.ReadJson("config/optional.json")
if err != nil {
fmt.Println("optional config not found, using defaults")
config = map[string]any{}
}
fmt.Println("config:", config)See also: eutil.ReadJson — Read JSON from disk filesystem (tolerant mode)
