gstring

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2018 License: MIT Imports: 2 Imported by: 0

README

gstring Build StatusCoverageGoDoc

Python like string formatting for golang.

Why?

Golang already has great features for string formatting (entire fmt package) I missed named parameters for string formatting. Intention of this library is to mimic subset of features that python's format function offers.

Example

Simple way of formatting string is using something like this:

// outputs "some value"
gstring.Printm("{key}", map[string]interface{}{"key": "some value"})

If bracked is needed in string it can be created by escaping (two brackets). Also, single key can appear multiple times in format string:

// outpits "Bracket {, }, key value, key value, key value"
gstring.Printm("Bracket {{, }}, {key}, {key}, {key}", map[string]interaface{}{"key", "key value"})

All standard formatting options from fmt work. To specify them, add colon after key name and specify fmt package compatible formatting optinos. For example:

// outpus "3.14"
gstring.Printm("{pi:%.2f}", map[string]interface{}{"pi": math.Pi})

Installation

Run go get github.com/delicb/gstring from command line.

Documentation

Overview

Gstring is alternative implementation of string formatting.

It is not intended to be replacement for fmt package from golang standard library, but it should enhance it.

Example (Basic)
Printm("{test}", map[string]interface{}{"test": "hello world"})
Output:

hello world
Example (Formatting)
Printm("{pi:%2.5f}", map[string]interface{}{"pi": math.Pi})
Output:

3.14159

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorm

func Errorm(format string, args map[string]interface{}) error

Errorm returns error instance with formatted error message. This is same as fmt.Errorf, but uses gstring formatting.

func Fprintm

func Fprintm(w io.Writer, format string, args map[string]interface{}) (n int, err error)

Fprintm writes formatted string to provided writer. This is same as fmt.Fprintf, but uses gstring formatting.

func Printm

func Printm(format string, args map[string]interface{}) (n int, err error)

Printf prints formatted string to stdout. This is same as fmt.Printf, but uses gstring formatting.

func Sprintm

func Sprintm(format string, args map[string]interface{}) string

Sprintf returns formatted string. This is same as fmt.Sprintf, but uses gstring formatting.

Types

This section is empty.

Jump to

Keyboard shortcuts

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