The Standard Library
Last updated
Was this helpful?
Last updated
Was this helpful?
Corgi also comes with a standard library, providing a handful of useful mixins:
Grouped formats the stringified version of val
in groups of size
runes.
Each group is separated by sep
.
If trailRem
is set, the remainder group, i.e. the group that has less than size
elements, will be the last group. Otherwise, the first group will be that group.
If minSize
is set and val
is shorter than minSize
, Grouped will print it as is.
Num is used for grouping numbers in groups of 3, if they are greater or equal to 10 000, or smaller or equal to -10 000.
Num handles three special cases that Grouped doesn't.
The sign isn't counted towards any group. Where Grouped would print - 123 456
, Num will print -123 456
.
Num correctly handles decimal points: 123456.7891
will be printed as 123 456.7891
and 12345.67891
will be printed as 12 345.67 890
.
If SetDigitGroupSep's _
block is set, the group separator will only be changed inside the block. Further calls to SetDigitGroupSep inside the block, even if without a _
block, will also be reverted.
If SetDecimalSep's _
block is set, the decimal separator will only be changed inside the block. Further calls to SetDecimalSep inside the block, even if without a _
block, will also be reverted.
If SetNumWords's _
block is set, the spelled-out words will only be changed inside the block. Further calls to SetNumWords inside the block, even if without a _
block, will also be reverted.
By default, words[0]
will be assumed to be zero. This can be changed by setting the start
param.
List formats the stringified elements of val
, which must be a slice, as a list.
If val
is not a slice, List will return from the generated function with an error.
The list will be constructed as follows:
If the slice is empty or nil, nothing will be printed.
If the slice contains a single element, only that will be printed.
If the slice contains two elements they will both be printed, separated by lastSep
.
If the slice contains more than two elements, all but the last will be printed, separated by sep
. Then, the last element will be printed, separated from the rest by lastSep
.
By default sep is ,
, and lastSep is , and
.
If SetListSeps's _
block is set, the spelled-out words will only be changed inside the block. Further calls to SetNumWords inside the block, even if without a _
block, will also be reverted.
If asIs
is true, the time won't be converted to another timezone.
SetTimeFormat sets the default global time format.
If left unchanged, it will be 15:04
.
SetDateFormat sets the default global date format.
If left unchanged, it will be 2006-01-02
.
If asIs
is true, the time won't be converted to another timezone.
SetDateTimeFormat sets the default global date time format.
If left unchanged, it will be 2006-01-02 15:04
.
Breaked replaces each newline in the stringified version of val
with a <br>
and prints it.
The optional maxAdjacent
arg allows to limit the number of adjacent <br>
s, to an arbitrary number. This means groups of more than maxAdjacent
consecutive newlines are replaced with only maxAdjacent
* <br>
.
Setting maxAdjacent
to 0
effectively removes all newlines from val
.
A negative value is the default and causes all <br>
s to be printed.
Trunc truncates the stringified version of val after the at
-th rune.
If at
is negative, Trunc will truncate the at
-th rune starting from the last rune.
If is bigger than or equal to the rune length of val
, then all of val
will be printed.
TruncWords truncates the stringified version of val
after max
words.
A word is a sequence of one or more characters that is separated from another such sequence by one or more of [ \t\r\n]
.
If max
is negative, TruncWords will truncate from the end of val
instead of from the beginning.
If , the stringified version of val
will be printed as is.
If at
is negative, Abbrev prepends replacement
instead of appending it.
By default, replacement
is U+2026
Horizontal Ellipsis.
Initials prints the first letter of each word in the stringified version of val
.
A word is a sequence of one or more characters that is separated from another such sequence by one or more of [ \t\r\n]
.
If max
is set to a number higher than 0
, Initials will print at most max
initials.
If upper
is set to true
, the initials will be uppercased before printing.
Similarly, if lower
is set to true
, the initials will be lowercase.
If both are false
, the initials will be printed as is, and if both are true
, upper
takes precedence.
Wrap wraps the stringified version of val
by printing with
unescaped every after
runes.
Nbsp replaces all spaces in the stringified version of val
with non-breaking spaces.
RandText produces a string of length
runes generated from the passed charset
.
The charset
may consist of character ranges such as a-z
, or literal characters.
If charset is set to an empty or invalid string, RandText returns with an error from the generated function.
If length
is negative or zero, RandText prints nothing. The generated text is cryptographically secure, utilizing crypto/rand
.
If useWords
is set to true
, Num will write out the number if it is in the range of 0-9. This means instead of printing 7
, Num will print seven
. That range and those numbers can be changed using .
By default, Num will separate groups with spaces, however, you may change this using groupSep
or by setting a global default using .
Num does not validate if val is actually a number. Instead, it operates on val's stringified version, scanning for .
as a decimal separator. When printing, Num uses that same separator, unless decimalSep
is set, or the global default was changed by calling .
SetDigitGroupSep sets the global default for the digit group separator used by .
SetDigitGroupSep may be called at any time, only affecting the calls to made after updating the separator.
SetDecimalSep sets the global default for the decimal separator used by .
SetDecimalSep may be called at any time, only affecting the calls to made after updating the separator.
SetNumWords sets the global default for the spelled-out numbers used by , if useWords
is set to true.
SetNumWords may be called at any time, only affecting the calls to made after updating the words.
You can change the defaults of sep
and lastSep
by calling .
SetListSep updates the global default for the sep
and lastSep
values used by . If you don't specify sep
or lastSep
, or assign them an empty value, they won't get updated.
SetListSep may be called at any time, only affecting the calls to made after updating the separator.
SetLocation updates the default location used for , , and .
Time formats t
according to format
, which defaults to the global time format, changeable through .
By default, the time is printed in the global default location, which by default is time.Local
, changeable trough .
Date formats t
according to format, which defaults to the global time format, changeable through .
Unlike and , the date will be printed in the timezone that t
is already in.
This can be changed by setting asIs
to true, after which, the date is printed in the global default location, which by default is time.Local
, changeable trough . This is usually only desired if t
is a datetime and you only want to print the date.
DateTime formats t
according to format, which defaults to the global time format, changeable through .
By default, the datetime is printed in the global default location, which by default is time.Local
, changeable trough .
Abbrev truncates the stringified version of val
according to the rules of and then appends replacement
, if it truncated anything.
AbbrevWords is the version of .