gn

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 5 Imported by: 5

README

gn

User-friendly messaging and error handling for Go with colorized output.

Installation

go get github.com/gnames/gn

Usage

Messages
import "github.com/gnames/gn"

gn.Message("Processing data...")           // no icon
gn.Info("Server started on port %d", 8080) // ℹ️
gn.Warn("Connection timeout")              // ⚠️
gn.Success("All tests passed!")            // ✅
gn.Progress("Downloading files...")        // ⏳
Inline Formatting
gn.Info("Starting <title>Production Server</title>")
gn.Message("Found <em>42</em> records")
gn.Info("Status: <warn>degraded</warn>")
gn.Info("Errors: <err>3</err>")

Available tags:

  • <title>...</title> - green with ** emphasis
  • <em>...</em> - green
  • <warn>...</warn> - yellow
  • <err>...</err> - red
Custom Errors
const (
    ErrDatabase gn.ErrorCode = 1000
    ErrNetwork  gn.ErrorCode = 2000
)

err := &gn.Error{
    Code: ErrDatabase,
    Err:  errors.New("connection failed"),
    Msg:  "Could not connect to database: %s",
    Vars: []any{"postgres"},
}

gn.PrintErrorMessage(err) // ❌ Could not connect to database: postgres

// Works with standard error handling
var gnErr *gn.Error
if errors.As(err, &gnErr) {
    fmt.Printf("Error code: %d\n", gnErr.Code)
}

License

See LICENSE file.

Documentation

Overview

Package gn provides user-friendly messaging and error handling utilities for Go applications with colorized output and formatted messages.

Package gn provides user-friendly messaging and error handling utilities for Go applications with colorized output and formatted messages.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Info

func Info(msg string, vars ...any)

Info prints an informational message with an info icon (ℹ️). The msg parameter can include format specifiers and inline formatting tags. Optional vars are used to format the message using fmt.Sprintf.

Example
Info("Processing <em>%d</em> items", 100)

func Message

func Message(msg string, vars ...any)

Message prints a general message without a specific type indicator. The msg parameter can include format specifiers and inline formatting tags. Optional vars are used to format the message using fmt.Sprintf.

Example
Message("This is a general message")

func PrintErrorMessage

func PrintErrorMessage(err error)

PrintErrorMessage attempts to print an error message if the error implements the Error interface. It uses errors.As to check if the error can be converted to the custom Error type, and if so, prints it with the appropriate formatting.

func Progress

func Progress(msg string, vars ...any)

Progress prints a progress message with a progress icon (⏳). The msg parameter can include format specifiers and inline formatting tags. Optional vars are used to format the message using fmt.Sprintf.

Example
Progress("Loading <title>data</title>...")

func Success

func Success(msg string, vars ...any)

Success prints a success message with a success icon (✅). The msg parameter can include format specifiers and inline formatting tags. Optional vars are used to format the message using fmt.Sprintf.

Example
Success("Operation completed successfully!")

func Warn

func Warn(msg string, vars ...any)

Warn prints a warning message with a warning icon (⚠️). The msg parameter can include format specifiers and inline formatting tags. Optional vars are used to format the message using fmt.Sprintf.

Example
Warn("This is a warning: %s", "something went wrong")

Types

type Error

type Error struct {
	Code ErrorCode // Numeric code identifying the type of error
	Err  error     // The underlying error
	Msg  string    // User-friendly message template (can include format specifiers)
	Vars []any     // Variables to be formatted into the message template
}

Error is a custom error type that extends the standard error interface with additional fields for error codes, custom messages, and formatting variables. It can be used to create rich, user-friendly error messages.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface, returning the underlying error's message if available, or the custom message otherwise.

type ErrorCode

type ErrorCode int

ErrorCode represents a numeric code for categorizing errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL