Python like string formatting for golang
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
documentation.go
example_test.go
go.mod
gstring.go
gstring_test.go

README.md

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.