Skip to content

Introduction

Utility function module, provides JSON file reading (from disk filesystem), environment variable retrieval, and panic recovery, among other general functions. JSON reading supports strict mode (exits process on failure) and tolerant mode (returns error). The Recover() function captures panic and outputs error logs with complete stack traces.

Import

go
import "github.com/wallace5303/ee-go/eutil"

API

ReadJsonStrict(f)

Description: Read a JSON file from the disk filesystem and parse it into map[string]any. Strict mode: throws ExitConfigFileNotExist exception when the file doesn't exist, throws ExitConfigFile exception on read or parse failure, and the process exits directly. Suitable for configuration files that must exist.

Parameters:

ParameterTypeRequiredDescription
fstringYesJSON file path on disk

Returns: map[string]any — key-value mapping after parsing the JSON file

Example:

go
config := eutil.ReadJsonStrict("/path/to/config.json")
fmt.Println("database host:", config["host"])

See also: estatic.ReadJsonStrict — Read JSON from embedded filesystem (strict mode)

ReadJson(f)

Description: Read a JSON file from the disk filesystem and parse it into map[string]any. Tolerant mode: returns an error on read failure or JSON parse failure without exiting the process. Suitable for optional or potentially missing configuration files.

Parameters:

ParameterTypeRequiredDescription
fstringYesJSON file path on disk

Returns: (map[string]any, error) — on success returns JSON parse result and nil, on failure returns nil and error information

Example:

go
config, err := eutil.ReadJson("/path/to/optional.json")
if err != nil {
    fmt.Println("file not found, using defaults:", err)
    config = map[string]any{}
}
fmt.Println("config data:", config)

See also: estatic.ReadJson — Read JSON from embedded filesystem (tolerant mode)

GetEnvs()

Description: Get and print all environment variables of the current process. Iterates through the key-value pairs returned by os.Environ() and outputs them line by line to stdout. Mainly used for debugging and diagnostics.

Returns: No return value

Example:

go
eutil.GetEnvs()
// Output:
// HOME = /Users/user
// PATH = /usr/local/bin:/usr/bin
// ...

Recover()

Description: Capture panic exceptions, logging error information including complete stack traces. Stack trace information includes file name, line number, function name, and source code content. Should be called at the beginning of functions that may panic using defer eutil.Recover().

Returns: No return value

Example:

go
func riskyOperation() {
    defer eutil.Recover()
    // Operation that may panic
    panic("something went wrong")
}