本文整理汇总了Golang中github.com/spf13/cast.ToStringE函数的典型用法代码示例。如果您正苦于以下问题:Golang ToStringE函数的具体用法?Golang ToStringE怎么用?Golang ToStringE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToStringE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Delimit
func Delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, error) {
d, err := cast.ToStringE(delimiter)
if err != nil {
return "", err
}
var dLast *string
for _, l := range last {
dStr, err := cast.ToStringE(l)
if err != nil {
dLast = nil
}
dLast = &dStr
break
}
seqv := reflect.ValueOf(seq)
seqv, isNil := indirect(seqv)
if isNil {
return "", errors.New("can't iterate over a nil value")
}
var str string
switch seqv.Kind() {
case reflect.Map:
sortSeq, err := Sort(seq)
if err != nil {
return "", err
}
seqv = reflect.ValueOf(sortSeq)
fallthrough
case reflect.Array, reflect.Slice, reflect.String:
for i := 0; i < seqv.Len(); i++ {
val := seqv.Index(i).Interface()
valStr, err := cast.ToStringE(val)
if err != nil {
continue
}
switch {
case i == seqv.Len()-2 && dLast != nil:
str += valStr + *dLast
case i == seqv.Len()-1:
str += valStr
default:
str += valStr + d
}
}
default:
return "", errors.New("can't iterate over " + reflect.ValueOf(seq).Type().String())
}
return template.HTML(str), nil
}
示例2: Replace
// Replace all occurences of b with c in a
func Replace(a, b, c interface{}) (string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return "", err
}
bStr, err := cast.ToStringE(b)
if err != nil {
return "", err
}
cStr, err := cast.ToStringE(c)
if err != nil {
return "", err
}
return strings.Replace(aStr, bStr, cStr, -1), nil
}
示例3: String
func (self *Options) String(key string) string {
value, err := cast.ToStringE(self.Interface(key))
if err != nil {
self.log.Printf("%s for key '%s'", err.Error(), key)
}
return value
}
示例4: singularize
// singularize returns the singular form of a single word.
func singularize(in interface{}) (string, error) {
word, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return inflect.Singularize(word), nil
}
示例5: substr
// substr extracts parts of a string, beginning at the character at the specified
// position, and returns the specified number of characters.
//
// It normally takes two parameters: start and length.
// It can also take one parameter: start, i.e. length is omitted, in which case
// the substring starting from start until the end of the string will be returned.
//
// To extract characters from the end of the string, use a negative start number.
//
// In addition, borrowing from the extended behavior described at http://php.net/substr,
// if length is given and is negative, then that many characters will be omitted from
// the end of string.
func substr(a interface{}, nums ...interface{}) (string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return "", err
}
var start, length int
asRunes := []rune(aStr)
switch len(nums) {
case 0:
return "", errors.New("too less arguments")
case 1:
if start, err = cast.ToIntE(nums[0]); err != nil {
return "", errors.New("start argument must be integer")
}
length = len(asRunes)
case 2:
if start, err = cast.ToIntE(nums[0]); err != nil {
return "", errors.New("start argument must be integer")
}
if length, err = cast.ToIntE(nums[1]); err != nil {
return "", errors.New("length argument must be integer")
}
default:
return "", errors.New("too many arguments")
}
if start < -len(asRunes) {
start = 0
}
if start > len(asRunes) {
return "", fmt.Errorf("start position out of bounds for %d-byte string", len(aStr))
}
var s, e int
if start >= 0 && length >= 0 {
s = start
e = start + length
} else if start < 0 && length >= 0 {
s = len(asRunes) + start - length + 1
e = len(asRunes) + start + 1
} else if start >= 0 && length < 0 {
s = start
e = len(asRunes) + length
} else {
s = len(asRunes) + start
e = len(asRunes) + length
}
if s > e {
return "", fmt.Errorf("calculated start position greater than end position: %d > %d", s, e)
}
if e > len(asRunes) {
e = len(asRunes)
}
return string(asRunes[s:e]), nil
}
示例6: Param
// Param is a convenience method to do lookups in Site's Params map.
//
// This method is also implemented on Page.
func (n *Node) Param(key interface{}) (interface{}, error) {
keyStr, err := cast.ToStringE(key)
if err != nil {
return nil, err
}
return n.Site.Params[keyStr], err
}
示例7: Trim
// Trim leading/trailing characters defined by b from a
func Trim(a interface{}, b string) (string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return "", err
}
return strings.Trim(aStr, b), nil
}
示例8: htmlUnescape
func htmlUnescape(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return html.UnescapeString(conv), nil
}
示例9: Slicestr
// Slicing in Slicestr is done by specifying a half-open range with
// two indices, start and end. 1 and 4 creates a slice including elements 1 through 3.
// The end index can be omitted, it defaults to the string's length.
func Slicestr(a interface{}, startEnd ...int) (string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return "", err
}
if len(startEnd) > 2 {
return "", errors.New("too many arguments")
}
asRunes := []rune(aStr)
if len(startEnd) > 0 && (startEnd[0] < 0 || startEnd[0] >= len(asRunes)) {
return "", errors.New("slice bounds out of range")
}
if len(startEnd) == 2 {
if startEnd[1] < 0 || startEnd[1] > len(asRunes) {
return "", errors.New("slice bounds out of range")
}
return string(asRunes[startEnd[0]:startEnd[1]]), nil
} else if len(startEnd) == 1 {
return string(asRunes[startEnd[0]:]), nil
} else {
return string(asRunes[:]), nil
}
}
示例10: Split
func Split(a interface{}, delimiter string) ([]string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return []string{}, err
}
return strings.Split(aStr, delimiter), nil
}
示例11: String
func (c *Configr) String(key string) (string, error) {
val, err := c.Get(key)
if err != nil {
return "", err
}
return cast.ToStringE(val)
}
示例12: emojify
// emojify "emojifies" the given string.
//
// See http://www.emoji-cheat-sheet.com/
func emojify(in interface{}) (template.HTML, error) {
str, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return template.HTML(helpers.Emojify([]byte(str))), nil
}
示例13: Chomp
func Chomp(text interface{}) (string, error) {
s, err := cast.ToStringE(text)
if err != nil {
return "", err
}
return strings.TrimRight(s, "\r\n"), nil
}
示例14: chomp
// chomp removes trailing newline characters from a string.
func chomp(text interface{}) (template.HTML, error) {
s, err := cast.ToStringE(text)
if err != nil {
return "", err
}
return template.HTML(strings.TrimRight(s, "\r\n")), nil
}
示例15: sha1
// sha1 hashes the given input and returns its SHA1 checksum
func sha1(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return "", err
}
hash := _sha1.Sum([]byte(conv))
return hex.EncodeToString(hash[:]), nil
}