stringfrom

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

stringfrom

Convert pointers and other values to strings for display, with a chosen text for nil. Use it to render optional fields in templates, CSV files or log messages without writing a nil check for every field.

import "github.com/domonda/go-types/strutil/stringfrom"

Pointer functions

Every *Ptr function takes a pointer and optional strForNil ...string arguments. For a nil pointer it returns the strForNil arguments joined without a separator, so Ptr(nil) is "", Ptr(nil, "n/a") is "n/a" and Ptr(nil, "<", "nil", ">") is "<nil>".

Function Non-nil result Returns strForNil for
Ptr(ptr *string, strForNil ...string) string *ptr nil
TimePtr(ptr *time.Time, strForNil ...string) string ptr.String(), like "2024-03-01 12:30:00 +0000 UTC" nil and a pointer to the zero time
UUIDPtr(ptr *uu.ID, strForNil ...string) string ptr.String(), the canonical UUID form nil only, a pointer to uu.IDNil gives "00000000-0000-0000-0000-000000000000"
IntPtr(ptr *int, strForNil ...string) string decimal, like "-3" nil
UintPtr(ptr *uint, strForNil ...string) string decimal, like "7" nil
Int64Ptr(ptr *int64, strForNil ...string) string decimal nil
Float64Ptr(ptr *float64, strForNil ...string) string strconv.FormatFloat(*ptr, 'f', -1, 64): no exponent, like "0.1" or "1000000000000000000000" for 1e21; "NaN", "+Inf", "-Inf" nil
BoolPtr(ptr *bool, strForNil ...string) string "true" or "false" nil

Other functions

Function Description
Bool(boolVal bool, trueString, falseString string) string trueString if boolVal is true, else falseString.
Interface(i any) string Converts i, or the value i points to: a value with a Format, Error or String method, also one with a pointer receiver, with that method, a value of string kind as is, any other value with fmt.Sprint. A non-pointer value gives the same string as a pointer to it. Returns "" for nil, a nil pointer, and a nil map, slice, func, channel or interface value.

None of the functions panics for nil input.

Gotchas

  • TimePtr uses time.Time.String, which is not RFC 3339 and includes the monotonic clock reading for times from time.Now(), like "2026-10-12 14:15:32.261641 +0200 CEST m=+0.000682459". Call Round(0) on the time or format it yourself if you need a stable layout.
  • UUIDPtr does not treat uu.IDNil as nil, unlike TimePtr with the zero time.
  • Interface dereferences only one level: a pointer to a non-nil pointer prints as an address.

Example

package main

import (
	"fmt"
	"time"

	"github.com/domonda/go-types/strutil/stringfrom"
	"github.com/domonda/go-types/uu"
)

func main() {
	var name *string
	fmt.Println(stringfrom.Ptr(name, "<unknown>"))

	paid := time.Date(2024, 3, 1, 12, 30, 0, 0, time.UTC)
	var zero time.Time
	fmt.Println(stringfrom.TimePtr(&paid, "unpaid"))
	fmt.Println(stringfrom.TimePtr(&zero, "unpaid"))

	fmt.Println(stringfrom.UUIDPtr(nil, "no ID"))
	fmt.Println(stringfrom.UUIDPtr(&uu.IDNil, "no ID"))

	amount := 1234.5
	fmt.Println(stringfrom.Float64Ptr(&amount))
	fmt.Println(stringfrom.Bool(true, "yes", "no"))

	n := 42
	fmt.Printf("%q %q %q\n", stringfrom.Interface(&n), stringfrom.Interface(n), stringfrom.Interface(nil))
	// Output:
	// <unknown>
	// 2024-03-01 12:30:00 +0000 UTC
	// unpaid
	// no ID
	// 00000000-0000-0000-0000-000000000000
	// 1234.5
	// yes
	// "42" "42" ""
}
  • strutil: string helpers of the parent package, including DerefPtr and StringToPtrEmptyToNil for *string.
  • Root package reference: types.FromPtr and types.FromPtrOr for pointers of any type. Go 1.26's new(expr) replaces the removed Ptr functions.
  • uu: the uu.ID type formatted by UUIDPtr.
  • nullable: optional values as types instead of pointers.
  • How to store optional values

Documentation

Overview

Package stringfrom provides utilities for converting various Go types to strings with safe handling of nil pointers and customizable default values.

The package includes:

  • Ptr, TimePtr, UUIDPtr, IntPtr, UintPtr, Int64Ptr, Float64Ptr and BoolPtr, which dereference a pointer and format the value. For a nil pointer they return their strForNil arguments joined without a separator, or "" if none are passed. TimePtr treats a pointer to the zero time like nil.
  • Bool, which returns one of two passed strings.
  • Interface, which converts any value, or the value a pointer points to: a value with a Format, Error or String method, also one with a pointer receiver, with that method, a value of string kind as is, other values with fmt.Sprint. It returns "" for nil, a nil pointer, or a nil map, slice, func, channel or interface value.

None of the functions panics for nil input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(boolVal bool, trueString, falseString string) string

Bool converts a boolean value to a string using custom true/false strings.

func BoolPtr

func BoolPtr(ptr *bool, strForNil ...string) string

BoolPtr safely dereferences a *bool pointer and returns "true"/"false" string or a custom string for nil.

func Float64Ptr

func Float64Ptr(ptr *float64, strForNil ...string) string

Float64Ptr safely dereferences a *float64 pointer and returns its string representation or a custom string for nil.

func Int64Ptr

func Int64Ptr(ptr *int64, strForNil ...string) string

Int64Ptr safely dereferences a *int64 pointer and returns its string representation or a custom string for nil.

func IntPtr

func IntPtr(ptr *int, strForNil ...string) string

IntPtr safely dereferences a *int pointer and returns its string representation or a custom string for nil.

func Interface

func Interface(i any) string

Interface converts i, or the value i points to, to a string, so a non-pointer value returns the same string as a pointer to it. If the value or a pointer to it has a Format, Error or String method, the value is formatted with fmt.Sprint through a pointer to it, so methods with a pointer receiver are used too. Otherwise a value of string kind is returned as is, and other values are formatted with fmt.Sprint. Returns an empty string for nil, a nil pointer, or a nil map, slice, func, channel or interface value.

func Ptr

func Ptr(ptr *string, strForNil ...string) string

Ptr safely dereferences a *string pointer and returns the value or a custom string for nil. If ptr is nil, returns the concatenation of strForNil arguments.

func TimePtr

func TimePtr(ptr *time.Time, strForNil ...string) string

TimePtr safely dereferences a *time.Time pointer and returns its string representation or a custom string for nil/zero values.

func UUIDPtr

func UUIDPtr(ptr *uu.ID, strForNil ...string) string

UUIDPtr safely dereferences a *uu.ID pointer and returns its string representation or a custom string for nil.

func UintPtr

func UintPtr(ptr *uint, strForNil ...string) string

UintPtr safely dereferences a *uint pointer and returns its string representation or a custom string for nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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