Skip to content

Helpers Package

Generic utilities - Portable helper functions with no external dependencies.


The Helpers package (packages/helpers/) provides generic utility functions that can be used in any Go project. It has no external dependencies beyond the standard library.


packages/helpers/
├── config.go # Configuration parsing
├── sanitizer.go # JSON/JSONC sanitization
└── pointers.go # Pointer helpers

Parse string with fallback value:

func ParseStringWithDefault(value string, def string) string

Usage:

// Returns "default" if value is empty
result := helpers.ParseStringWithDefault("", "default") // "default"
result := helpers.ParseStringWithDefault("value", "default") // "value"
result := helpers.ParseStringWithDefault(" ", "default") // "default" (trimmed)

Parse integer with fallback:

func ParseIntWithDefault(value string, def int) int

Usage:

result := helpers.ParseIntWithDefault("42", 0) // 42
result := helpers.ParseIntWithDefault("", 0) // 0
result := helpers.ParseIntWithDefault("abc", 0) // 0 (invalid)

Parse boolean with fallback:

func ParseBoolWithDefault(value string, def bool) bool

Supported true values: 1, true, yes, on Supported false values: 0, false, no, off

Usage:

result := helpers.ParseBoolWithDefault("true", false) // true
result := helpers.ParseBoolWithDefault("yes", false) // true
result := helpers.ParseBoolWithDefault("1", false) // true
result := helpers.ParseBoolWithDefault("", false) // false
result := helpers.ParseBoolWithDefault("invalid", false) // false

Handle JSON with Comments (JSONC) format.

Remove comments from JSONC string:

func SanitizeJSONC(jsonc string) string

Features:

  • Removes single-line comments (//)
  • Removes multi-line comments (/* */)
  • Removes trailing commas
  • Preserves comments inside strings
  • Handles edge cases

Usage:

jsonc := `{
// This is a comment
"name": "Test",
"value": 42, // inline comment
/* Multi-line
comment */
"enabled": true,
}`
clean := helpers.SanitizeJSONC(jsonc)
// Returns valid JSON without comments

Parse JSONC to map:

func ParseJSONC(jsonc string) (map[string]interface{}, error)

Usage:

data, err := helpers.ParseJSONC(`{
// Config file
"name": "MyApp",
"version": "1.0"
}`)
// data = map[string]interface{}{"name": "MyApp", "version": "1.0"}

Parse JSONC to array:

func ParseJSONCArray(jsonc string) ([]interface{}, error)

Usage:

data, err := helpers.ParseJSONCArray(`[
// Items
{"id": 1},
{"id": 2},
]`)

Parse and re-stringify to clean JSON:

func ParseJSONCToString(jsonc string) (string, error)

Usage:

clean, err := helpers.ParseJSONCToString(`{
"name": "Test", // comment
}`)
// Returns formatted JSON string without comments

Generic JSONC to type parsing:

func ParseJSONCToType[T any](jsonc string) (*T, error)

Usage:

type Config struct {
Name string `json:"name"`
Version string `json:"version"`
}
config, err := helpers.ParseJSONCToType[Config](`{
"name": "MyApp",
"version": "1.0"
}`)

Convert map to JSON string:

func ParseJsonToString(m map[string]any) (string, error)

Usage:

data := map[string]any{"name": "Test", "count": 42}
jsonStr, err := helpers.ParseJsonToString(data)
// jsonStr = `{"count":42,"name":"Test"}`

import "gitlab.com/altacrest/flow_binaries/packages/helpers"

Use CaseFunction
Environment variablesParseStringWithDefault, ParseIntWithDefault
Configuration filesParseBoolWithDefault
JSONC config filesParseJSONC, SanitizeJSONC
API responsesParseJSONCToType
Dynamic dataParseJsonToString

import "os"
import "gitlab.com/altacrest/flow_binaries/packages/helpers"
func LoadConfig() Config {
return Config{
Port: helpers.ParseIntWithDefault(os.Getenv("PORT"), 8080),
Host: helpers.ParseStringWithDefault(os.Getenv("HOST"), "localhost"),
Debug: helpers.ParseBoolWithDefault(os.Getenv("DEBUG"), false),
}
}
func LoadJSONCConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return helpers.ParseJSONCToType[Config](string(data))
}

This package has no external dependencies - only uses Go standard library.