Documentation ¶
Overview ¶
Package str is a comprehensive set of string functions to build more Go awesomeness. Str complements Go's standard packages and does not duplicate functionality found in `strings` or `strconv`.
Str is based on plain functions instead of object-based methods, consistent with Go standard string packages.
str.Between("<a>foo</a>", "<a>", "</a>") == "foo"
Str supports pipelining instead of chaining
s := str.Pipe("\nabcdef\n", Clean, BetweenF("a", "f"), ChompLeftF("bc"))
User-defined filters can be added to the pipeline by inserting a function or closure that returns a function with this signature
func(string) string
Index ¶
- Variables
- func Between(s, left, right string) string
- func BetweenF(left, right string) func(string) string
- func Camelize(s string) string
- func Capitalize(s string) string
- func CharAt(s string, index int) string
- func CharAtF(index int) func(string) string
- func ChompLeft(s, prefix string) string
- func ChompLeftF(prefix string) func(string) string
- func ChompRight(s, suffix string) string
- func ChompRightF(suffix string) func(string) string
- func Classify(s string) string
- func ClassifyF(s string) func(string) string
- func Clean(s string) string
- func Dasherize(s string) string
- func DecodeHTMLEntities(s string) string
- func EnsurePrefix(s, prefix string) string
- func EnsurePrefixF(prefix string) func(string) string
- func EnsureSuffix(s, suffix string) string
- func EnsureSuffixF(suffix string) func(string) string
- func EscapeHTML(s string) string
- func Humanize(s string) string
- func Iif(condition bool, truthy string, falsey string) string
- func IndexOf(s string, needle string, start int) int
- func IsAlpha(s string) bool
- func IsAlphaNumeric(s string) bool
- func IsEmpty(s string) bool
- func IsLower(s string) bool
- func IsNumeric(s string) bool
- func IsUpper(s string) bool
- func Left(s string, n int) string
- func LeftF(n int) func(string) string
- func LeftOf(s string, needle string) string
- func Letters(s string) []string
- func Lines(s string) []string
- func Map(arr []string, iterator func(string) string) []string
- func Match(s, pattern string) bool
- func Pad(s, c string, n int) string
- func PadF(c string, n int) func(string) string
- func PadLeft(s, c string, n int) string
- func PadLeftF(c string, n int) func(string) string
- func PadRight(s, c string, n int) string
- func PadRightF(c string, n int) func(string) string
- func Pipe(s string, funcs ...func(string) string) string
- func QuoteItems(arr []string) []string
- func ReplaceF(old, new string, n int) func(string) string
- func ReplacePattern(s, pattern, repl string) string
- func ReplacePatternF(pattern, repl string) func(string) string
- func Reverse(s string) string
- func Right(s string, n int) string
- func RightF(n int) func(string) string
- func RightOf(s string, prefix string) string
- func SetTemplateDelimiters(opening, closing string)
- func Slice(s string, start, end int) string
- func SliceF(start, end int) func(string) string
- func Slugify(s string) string
- func StripPunctuation(s string) string
- func StripTags(s string, tags ...string) string
- func Substr(s string, index int, n int) string
- func SubstrF(index, n int) func(string) string
- func Template(s string, values map[string]interface{}) string
- func TemplateDelimiters() (opening string, closing string)
- func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string
- func ToArgv(s string) []string
- func ToBool(s string) bool
- func ToBoolOr(s string, defaultValue bool) bool
- func ToFloat32Or(s string, defaultValue float32) float32
- func ToFloat64Or(s string, defaultValue float64) float64
- func ToIntOr(s string, defaultValue int) int
- func Underscore(s string) string
- func UnescapeHTML(s string) string
- func WrapHTML(s string, tag string, attrs map[string]string) string
- func WrapHTMLF(tag string, attrs map[string]string) func(string) string
Examples ¶
- Between
- BetweenF
- Camelize
- Capitalize
- CharAt
- CharAtF
- ChompLeft
- ChompLeftF
- ChompRight
- ChompRightF
- Classify
- Clean
- Dasherize
- DecodeHTMLEntities
- EnsurePrefix
- EnsurePrefixF
- EnsureSuffix
- Humanize
- Iif
- IndexOf
- IsAlpha
- IsAlphaNumeric
- IsEmpty
- IsLower
- IsNumeric
- IsUpper
- Left
- LeftOf
- Lines
- Match
- Pad
- PadLeft
- PadRight
- Pipe
- ReplaceF
- ReplacePattern
- ReplacePatternF
- Reverse
- Right
- RightF
- RightOf
- Slugify
- StripPunctuation
- StripTags
- Substr
- Template
- TemplateWithDelimiters
- ToArgv
- ToBool
- ToBoolOr
- ToIntOr
- Underscore
- WrapHTML
- WrapHTMLF
Constants ¶
This section is empty.
Variables ¶
var ToFloatOr = ToFloat64Or
ToFloatOr parses as a float64 or returns defaultValue.
var Verbose = false
Verbose flag enables console output for those functions that have counterparts in Go's excellent stadard packages.
Functions ¶
func Between ¶
Between extracts a string between left and right strings.
Example ¶
eg(1, Between("<a>foo</a>", "<a>", "</a>")) eg(2, Between("<a>foo</a></a>", "<a>", "</a>")) eg(3, Between("<a><a>foo</a></a>", "<a>", "</a>")) eg(4, Between("<a><a>foo</a></a>", "<a>", "</a>")) eg(5, Between("<a>foo", "<a>", "</a>")) eg(6, Between("Some strings } are very {weird}, dont you think?", "{", "}")) eg(7, Between("This is ateststring", "", "test")) eg(8, Between("This is ateststring", "test", ""))
Output: 1: foo 2: foo 3: <a>foo 4: <a>foo 5: 6: weird 7: This is a 8: string
func BetweenF ¶
BetweenF is the filter form for Between.
Example ¶
eg(1, Pipe("abc", BetweenF("a", "c")))
Output: 1: b
func Camelize ¶
Camelize return new string which removes any underscores or dashes and convert a string into camel casing.
Example ¶
eg(1, Camelize("data_rate")) eg(2, Camelize("background-color")) eg(3, Camelize("-moz-something")) eg(4, Camelize("_car_speed_")) eg(5, Camelize("yes_we_can"))
Output: 1: dataRate 2: backgroundColor 3: MozSomething 4: CarSpeed 5: yesWeCan
func Capitalize ¶
Capitalize uppercases the first char of s and lowercases the rest.
Example ¶
eg(1, Capitalize("abc")) eg(2, Capitalize("ABC"))
Output: 1: Abc 2: Abc
func CharAt ¶
CharAt returns a string from the character at the specified position.
Example ¶
eg(1, CharAt("abc", 1)) eg(2, CharAt("", -1)) eg(3, CharAt("", 0)) eg(4, CharAt("", 10)) eg(5, CharAt("abc", -1)) eg(6, CharAt("abc", 10))
Output: 1: b 2: 3: 4: 5: 6:
func CharAtF ¶
CharAtF is the filter form of CharAt.
Example ¶
eg(1, Pipe("abc", CharAtF(1)))
Output: 1: b
func ChompLeft ¶
ChompLeft removes prefix at the start of a string.
Example ¶
eg(1, ChompLeft("foobar", "foo")) eg(2, ChompLeft("foobar", "bar")) eg(3, ChompLeft("", "foo")) eg(4, ChompLeft("", "")) eg(5, ChompLeft("foo", ""))
Output: 1: bar 2: foobar 3: 4: 5: foo
func ChompLeftF ¶
ChompLeftF is the filter form of ChompLeft.
Example ¶
eg(1, Pipe("abc", ChompLeftF("ab")))
Output: 1: c
func ChompRight ¶
ChompRight removes suffix from end of s.
Example ¶
eg(1, ChompRight("foobar", "foo")) eg(2, ChompRight("foobar", "bar")) eg(3, ChompRight("", "foo")) eg(4, ChompRight("", ""))
Output: 1: foobar 2: foo 3: 4:
func ChompRightF ¶
ChompRightF is the filter form of ChompRight.
Example ¶
eg(1, Pipe("abc", ChompRightF("bc")))
Output: 1: a
func Classify ¶
Classify returns a camelized string with the first letter upper cased.
Example ¶
eg(1, Classify("data_rate")) eg(2, Classify("background-color")) eg(3, Classify("-moz-something")) eg(4, Classify("_car_speed_")) eg(5, Classify("yes_we_can"))
Output: 1: DataRate 2: BackgroundColor 3: MozSomething 4: CarSpeed 5: YesWeCan
func Clean ¶
Clean compresses all adjacent whitespace to a single space and trims s.
Example ¶
eg(1, Clean("clean")) eg(2, Clean("")) eg(3, Clean(" please\t clean \t \n me "))
Output: 1: clean 2: 3: please clean me
func Dasherize ¶
Dasherize converts a camel cased string into a string delimited by dashes.
Example ¶
eg(1, Dasherize("dataRate")) eg(2, Dasherize("CarSpeed")) eg(3, Dasherize("yesWeCan")) eg(4, Dasherize("")) eg(5, Dasherize("ABC"))
Output: 1: data-rate 2: -car-speed 3: yes-we-can 4: 5: -a-b-c
func DecodeHTMLEntities ¶
DecodeHTMLEntities decodes HTML entities into their proper string representation. DecodeHTMLEntities is an alias for html.UnescapeString
Example ¶
eg(1, DecodeHTMLEntities("Ken Thompson & Dennis Ritchie")) eg(2, DecodeHTMLEntities("3 < 4")) eg(3, DecodeHTMLEntities("http://"))
Output: 1: Ken Thompson & Dennis Ritchie 2: 3 < 4 3: http://
func EnsurePrefix ¶
EnsurePrefix ensures s starts with prefix.
Example ¶
eg(1, EnsurePrefix("foobar", "foo")) eg(2, EnsurePrefix("bar", "foo")) eg(3, EnsurePrefix("", "")) eg(4, EnsurePrefix("foo", "")) eg(5, EnsurePrefix("", "foo"))
Output: 1: foobar 2: foobar 3: 4: foo 5: foo
func EnsurePrefixF ¶
EnsurePrefixF is the filter form of EnsurePrefix.
Example ¶
eg(1, Pipe("dir", EnsurePrefixF("./")))
Output: 1: ./dir
func EnsureSuffix ¶
EnsureSuffix ensures s ends with suffix.
Example ¶
eg(1, EnsureSuffix("foobar", "bar")) eg(2, EnsureSuffix("foo", "bar")) eg(3, EnsureSuffix("", "")) eg(4, EnsureSuffix("foo", "")) eg(5, EnsureSuffix("", "bar"))
Output: 1: foobar 2: foobar 3: 4: foo 5: bar
func EnsureSuffixF ¶
EnsureSuffixF is the filter form of EnsureSuffix.
func Humanize ¶
Humanize transforms s into a human friendly form.
Example ¶
eg(1, Humanize("the_humanize_string_method")) eg(2, Humanize("ThehumanizeStringMethod")) eg(3, Humanize("the humanize string method"))
Output: 1: The humanize string method 2: Thehumanize string method 3: The humanize string method
func Iif ¶
Iif is short for immediate if. If condition is true return truthy else falsey.
Example ¶
eg(1, Iif(true, "T", "F")) eg(2, Iif(false, "T", "F"))
Output: 1: T 2: F
func IndexOf ¶
IndexOf finds the index of needle in s starting from start.
Example ¶
eg(1, IndexOf("abcdef", "a", 0)) eg(2, IndexOf("abcdef", "a", 3)) eg(3, IndexOf("abcdef", "a", -2)) eg(4, IndexOf("abcdef", "a", 10)) eg(5, IndexOf("", "a", 0)) eg(6, IndexOf("abcdef", "", 2)) eg(7, IndexOf("abcdef", "", 1000))
Output: 1: 0 2: -1 3: -1 4: -1 5: -1 6: 2 7: 6
func IsAlpha ¶
IsAlpha returns true if a string contains only letters from ASCII (a-z,A-Z). Other letters from other languages are not supported.
Example ¶
eg(1, IsAlpha("afaf")) eg(2, IsAlpha("FJslfjkasfs")) eg(3, IsAlpha("áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ")) eg(4, IsAlpha("adflj43faljsdf")) eg(5, IsAlpha("33")) eg(6, IsAlpha("TT....TTTafafetstYY")) eg(7, IsAlpha("-áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ"))
Output: 1: true 2: true 3: true 4: false 5: false 6: false 7: false
func IsAlphaNumeric ¶
IsAlphaNumeric returns true if a string contains letters and digits.
Example ¶
eg(1, IsAlphaNumeric("afaf35353afaf")) eg(2, IsAlphaNumeric("FFFF99fff")) eg(3, IsAlphaNumeric("99")) eg(4, IsAlphaNumeric("afff")) eg(5, IsAlphaNumeric("Infinity")) eg(6, IsAlphaNumeric("áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ1234567890")) eg(7, IsAlphaNumeric("-Infinity")) eg(8, IsAlphaNumeric("-33")) eg(9, IsAlphaNumeric("aaff..")) eg(10, IsAlphaNumeric(".áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ1234567890"))
Output: 1: true 2: true 3: true 4: true 5: true 6: true 7: false 8: false 9: false 10: false
func IsEmpty ¶
IsEmpty returns true if the string is solely composed of whitespace.
Example ¶
eg(1, IsEmpty(" ")) eg(2, IsEmpty("\t\t\t ")) eg(3, IsEmpty("\t\n ")) eg(4, IsEmpty("hi"))
Output: 1: true 2: true 3: true 4: false
func IsLower ¶
IsLower returns true if s comprised of all lower case characters.
Example ¶
eg(1, IsLower("a")) eg(2, IsLower("A")) eg(3, IsLower("abc")) eg(4, IsLower("aBc")) eg(5, IsLower("áéúóúãõàèìòùâêîôûäëïöüç")) eg(6, IsLower("hi jp")) eg(7, IsLower("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÇ")) eg(8, IsLower("áéúóúãõàèìòùâêîôûäëïöüçÁ")) eg(9, IsLower("áéúóúãõàèìòùâêîôû äëïöüç"))
Output: 1: true 2: false 3: true 4: false 5: true 6: false 7: false 8: false 9: false
func IsNumeric ¶
IsNumeric returns true if a string contains only digits from 0-9. Other digits not in Latin (such as Arabic) are not currently supported.
Example ¶
eg(1, IsNumeric("3")) eg(2, IsNumeric("34.22")) eg(3, IsNumeric("-22.33")) eg(4, IsNumeric("NaN")) eg(5, IsNumeric("Infinity")) eg(6, IsNumeric("-Infinity")) eg(7, IsNumeric("JP")) eg(8, IsNumeric("-5")) eg(9, IsNumeric("00099242424"))
Output: 1: true 2: false 3: false 4: false 5: false 6: false 7: false 8: false 9: true
func IsUpper ¶
IsUpper returns true if s contains all upper case chracters.
Example ¶
eg(1, IsUpper("a")) eg(2, IsUpper("A")) eg(3, IsUpper("ABC")) eg(4, IsUpper("aBc")) eg(5, IsUpper("áéúóúãõàèìòùâêîôûäëïöüç")) eg(6, IsUpper("HI JP")) eg(7, IsUpper("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÇ")) eg(8, IsUpper("áéúóúãõàèìòùâêîôûäëïöüçÁ")) eg(9, IsUpper("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎ ÔÛÄËÏÖÜÇ"))
Output: 1: false 2: true 3: true 4: false 5: false 6: false 7: true 8: false 9: false
func Left ¶
Left returns the left substring of length n.
Example ¶
eg(1, Left("abcdef", 0)) eg(2, Left("abcdef", 1)) eg(3, Left("abcdef", 4)) eg(4, Left("abcdef", -2))
Output: 1: 2: a 3: abcd 4: ef
func LeftOf ¶
LeftOf returns the substring left of needle.
Example ¶
eg(1, LeftOf("abcdef", "def")) eg(2, LeftOf("abcdef", "abc")) eg(3, LeftOf("abcdef", "")) eg(4, LeftOf("", "abc")) eg(5, LeftOf("abcdef", "xyz"))
Output: 1: abc 2: 3: abcdef 4: 5:
func Lines ¶
Lines convert windows newlines to unix newlines then convert to an Array of lines.
Example ¶
eg(1, Lines("a\r\nb\nc\r\n")) eg(2, Lines("a\r\nb\nc\r\nd"))
Output: 1: [a b c ] 2: [a b c d]
func Match ¶
Match returns true if patterns matches the string
Example ¶
eg(1, Match("foobar", `^fo.*r$`)) eg(2, Match("foobar", `^fo.*x$`)) eg(3, Match("", `^fo.*x$`))
Output: 1: true 2: false 3: false
func Pad ¶
Pad pads string s on both sides with c until it has length of n.
Example ¶
eg(1, Pad("hello", "x", 5)) eg(2, Pad("hello", "x", 10)) eg(3, Pad("hello", "x", 11)) eg(4, Pad("hello", "x", 6)) eg(5, Pad("hello", "x", 1))
Output: 1: hello 2: xxxhelloxx 3: xxxhelloxxx 4: xhello 5: hello
func PadLeft ¶
PadLeft pads s on left side with c until it has length of n.
Example ¶
eg(1, PadLeft("hello", "x", 5)) eg(2, PadLeft("hello", "x", 10)) eg(3, PadLeft("hello", "x", 11)) eg(4, PadLeft("hello", "x", 6)) eg(5, PadLeft("hello", "x", 1))
Output: 1: hello 2: xxxxxhello 3: xxxxxxhello 4: xhello 5: hello
func PadRight ¶
PadRight pads s on right side with c until it has length of n.
Example ¶
eg(1, PadRight("hello", "x", 5)) eg(2, PadRight("hello", "x", 10)) eg(3, PadRight("hello", "x", 11)) eg(4, PadRight("hello", "x", 6)) eg(5, PadRight("hello", "x", 1))
Output: 1: hello 2: helloxxxxx 3: helloxxxxxx 4: hellox 5: hello
func Pipe ¶
Pipe pipes s through one or more string filters.
Example ¶
eg(1, Pipe("\nabcdef \n", Clean, BetweenF("a", "f"), ChompLeftF("bc")))
Output: 1: de
func QuoteItems ¶
QuoteItems quotes all items in array, mostly for debugging.
func ReplaceF ¶
ReplaceF is the filter form of strings.Replace.
Example ¶
eg(1, Pipe("abcdefab", ReplaceF("ab", "x", -1))) eg(2, Pipe("abcdefab", ReplaceF("ab", "x", 1))) eg(3, Pipe("abcdefab", ReplaceF("ab", "x", 0)))
Output: 1: xcdefx 2: xcdefab 3: abcdefab
func ReplacePattern ¶
ReplacePattern replaces string with regexp string. ReplacePattern returns a copy of src, replacing matches of the Regexp with the replacement string repl. Inside repl, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.
Example ¶
eg(1, ReplacePattern("aabbcc", `a`, "x"))
Output: 1: xxbbcc
func ReplacePatternF ¶
ReplacePatternF is the filter form of ReplaceRegexp.
Example ¶
eg(1, Pipe("aabbcc", ReplacePatternF(`a`, "x")))
Output: 1: xxbbcc
func Reverse ¶
Reverse a string
Example ¶
eg(1, Reverse("abc")) eg(2, Reverse("中文"))
Output: 1: cba 2: 文中
func Right ¶
Right returns the right substring of length n.
Example ¶
eg(1, Right("abcdef", 0)) eg(2, Right("abcdef", 1)) eg(3, Right("abcdef", 4)) eg(4, Right("abcdef", -2))
Output: 1: 2: f 3: cdef 4: ab
func RightF ¶
RightF is the Filter version of Right.
Example ¶
eg(1, Pipe("abcdef", RightF(3)))
Output: 1: def
func RightOf ¶
RightOf returns the substring to the right of prefix.
Example ¶
eg(1, RightOf("abcdef", "abc")) eg(2, RightOf("abcdef", "def")) eg(3, RightOf("abcdef", "")) eg(4, RightOf("", "abc")) eg(5, RightOf("abcdef", "xyz"))
Output: 1: def 2: 3: abcdef 4: 5:
func SetTemplateDelimiters ¶
func SetTemplateDelimiters(opening, closing string)
SetTemplateDelimiters sets the delimiters for Template function. Defaults to "{{" and "}}"
func Slugify ¶
Slugify converts s into a dasherized string suitable for URL segment.
Example ¶
eg(1, Slugify("foo bar")) eg(2, Slugify("foo/bar bah")) eg(3, Slugify("foo-bar--bah"))
Output: 1: foo-bar 2: foobar-bah 3: foo-bar-bah
func StripPunctuation ¶
StripPunctuation strips puncation from string.
Example ¶
eg(1, StripPunctuation("My, st[ring] *full* of %punct)"))
Output: 1: My string full of punct
func StripTags ¶
StripTags strips all of the html tags or tags specified by the parameters
Example ¶
eg(1, StripTags("<p>just <b>some</b> text</p>")) eg(2, StripTags("<p>just <b>some</b> text</p>", "p")) eg(3, StripTags("<a><p>just <b>some</b> text</p></a>", "a", "p")) eg(4, StripTags("<a><p>just <b>some</b> text</p></a>", "b"))
Output: 1: just some text 2: just <b>some</b> text 3: just <b>some</b> text 4: <a><p>just some text</p></a>
func Substr ¶
Substr returns a substring of s starting at index of length n.
Example ¶
eg(1, Substr("abcdef", 2, -1)) eg(2, Substr("abcdef", 2, 0)) eg(3, Substr("abcdef", 2, 1)) eg(4, Substr("abcdef", 2, 3)) eg(5, Substr("abcdef", 2, 4)) eg(6, Substr("abcdef", 2, 100)) eg(7, Substr("abcdef", 0, 1))
Output: 1: 2: 3: c 4: cde 5: cdef 6: cdef 7: a
func Template ¶
Template is a string template which replaces template placeholders delimited by "{{" and "}}" with values from map. The global delimiters may be set with SetTemplateDelimiters.
Example ¶
eg(1, Template("Hello {{name}} at {{date-year}}", map[string]interface{}{"name": "foo", "date-year": 2014})) eg(2, Template("Hello {{name}}", map[string]interface{}{"name": ""})) SetTemplateDelimiters("{", "}") eg(3, Template("Hello {name} at {date-year}", map[string]interface{}{"name": "foo", "date-year": 2014}))
Output: 1: Hello foo at 2014 2: Hello 3: Hello foo at 2014
func TemplateDelimiters ¶
TemplateDelimiters is the getter for the opening and closing delimiters for Template.
func TemplateWithDelimiters ¶
func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string
TemplateWithDelimiters is string template with user-defineable opening and closing delimiters.
Example ¶
eg(1, TemplateWithDelimiters("Hello {{name}} at {{date-year}}", map[string]interface{}{"name": "foo", "date-year": 2014}, "{{", "}}")) eg(2, TemplateWithDelimiters("Hello #{name} at #{date-year}", map[string]interface{}{"name": "foo", "date-year": 2014}, "#{", "}")) eg(3, TemplateWithDelimiters("Hello (name) at (date-year)", map[string]interface{}{"name": "foo", "date-year": 2014}, "(", ")")) eg(4, TemplateWithDelimiters("Hello [name] at [date-year]", map[string]interface{}{"name": "foo", "date-year": 2014}, "[", "]")) eg(5, TemplateWithDelimiters("Hello *name* at *date-year*", map[string]interface{}{"name": "foo", "date-year": 2014}, "*", "*")) eg(6, TemplateWithDelimiters("Hello $name$ at $date-year$", map[string]interface{}{"name": "foo", "date-year": 2014}, "$", "$"))
Output: 1: Hello foo at 2014 2: Hello foo at 2014 3: Hello foo at 2014 4: Hello foo at 2014 5: Hello foo at 2014 6: Hello foo at 2014
func ToArgv ¶
ToArgv converts string s into an argv for exec.
Example ¶
eg(1, QuoteItems(ToArgv(`GO_ENV=test gosu --watch foo@release "some quoted string 'inside'"`))) eg(2, QuoteItems(ToArgv(`gosu foo\ bar`))) eg(3, QuoteItems(ToArgv(`gosu --test="some arg" -w -s a=123`)))
Output: 1: ["GO_ENV=test" "gosu" "--watch" "foo@release" "some quoted string 'inside'"] 2: ["gosu" "foo bar"] 3: ["gosu" "--test=some arg" "-w" "-s" "a=123"]
func ToBool ¶
ToBool fuzzily converts truthy values.
Example ¶
eg(1, ToBool("true")) eg(2, ToBool("yes")) eg(3, ToBool("1")) eg(4, ToBool("on")) eg(5, ToBool("false")) eg(6, ToBool("no")) eg(7, ToBool("0")) eg(8, ToBool("off")) eg(9, ToBool("")) eg(10, ToBool("?"))
Output: 1: true 2: true 3: true 4: true 5: false 6: false 7: false 8: false 9: false 10: false
func ToBoolOr ¶
ToBoolOr parses s as a bool or returns defaultValue.
Example ¶
eg(1, ToBoolOr("foo", true)) eg(2, ToBoolOr("foo", false)) eg(3, ToBoolOr("true", false)) eg(4, ToBoolOr("", true))
Output: 1: true 2: false 3: true 4: true
func ToFloat32Or ¶
ToFloat32Or parses as a float32 or returns defaultValue on error.
func ToFloat64Or ¶
ToFloat64Or parses s as a float64 or returns defaultValue.
func ToIntOr ¶
ToIntOr parses s as an int or returns defaultValue.
Example ¶
eg(1, ToIntOr("foo", 0)) eg(2, ToIntOr("", 1)) eg(3, ToIntOr("100", 0)) eg(4, ToIntOr("-1", 1))
Output: 1: 0 2: 1 3: 100 4: -1
func Underscore ¶
Underscore returns converted camel cased string into a string delimited by underscores.
Example ¶
eg(1, Underscore("fooBar")) eg(2, Underscore("FooBar")) eg(3, Underscore("")) eg(4, Underscore("x"))
Output: 1: foo_bar 2: _foo_bar 3: 4: x
func UnescapeHTML ¶
UnescapeHTML is an alias for html.UnescapeString.
func WrapHTML ¶
WrapHTML wraps s within HTML tag having attributes attrs. Note, WrapHTML does not escape s value.
Example ¶
eg(1, WrapHTML("foo", "span", nil)) eg(2, WrapHTML("foo", "", nil)) eg(3, WrapHTML("foo", "", map[string]string{"class": "bar"}))
Output: 1: <span>foo</span> 2: <div>foo</div> 3: <div class="bar">foo</div>
Types ¶
This section is empty.