• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang cast.ToStringE函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/spf13/cast.ToStringE函数的典型用法代码示例。如果您正苦于以下问题:Golang ToStringE函数的具体用法?Golang ToStringE怎么用?Golang ToStringE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ToStringE函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:iswarezwp,项目名称:hugo,代码行数:54,代码来源:template_funcs.go


示例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
}
开发者ID:iswarezwp,项目名称:hugo,代码行数:16,代码来源:template_funcs.go


示例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
}
开发者ID:thrawn01,项目名称:args,代码行数:7,代码来源:options.go


示例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
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:8,代码来源:template_funcs.go


示例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
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:72,代码来源:template_funcs.go


示例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
}
开发者ID:krig,项目名称:hugo,代码行数:10,代码来源:node.go


示例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
}
开发者ID:iswarezwp,项目名称:hugo,代码行数:8,代码来源:template_funcs.go


示例8: htmlUnescape

func htmlUnescape(in interface{}) (string, error) {
	conv, err := cast.ToStringE(in)
	if err != nil {
		return "", err
	}
	return html.UnescapeString(conv), nil
}
开发者ID:CowPanda,项目名称:hugo,代码行数:7,代码来源:template_funcs.go


示例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
	}

}
开发者ID:iswarezwp,项目名称:hugo,代码行数:31,代码来源:template_funcs.go


示例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
}
开发者ID:iswarezwp,项目名称:hugo,代码行数:7,代码来源:template_funcs.go


示例11: String

func (c *Configr) String(key string) (string, error) {
	val, err := c.Get(key)
	if err != nil {
		return "", err
	}
	return cast.ToStringE(val)
}
开发者ID:fdsolutions,项目名称:configr,代码行数:7,代码来源:configr.go


示例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
}
开发者ID:nitoyon,项目名称:hugo,代码行数:11,代码来源:template_funcs.go


示例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
}
开发者ID:iswarezwp,项目名称:hugo,代码行数:8,代码来源:template_funcs.go


示例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
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:9,代码来源:template_funcs.go


示例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
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:10,代码来源:template_funcs.go


示例16: highlight

// highlight returns an HTML string with syntax highlighting applied.
func highlight(in interface{}, lang, opts string) (template.HTML, error) {
	str, err := cast.ToStringE(in)

	if err != nil {
		return "", err
	}

	return template.HTML(helpers.Highlight(html.UnescapeString(str), lang, opts)), nil
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:10,代码来源:template_funcs.go


示例17: plainify

// plainify strips any HTML and returns the plain text version.
func plainify(in interface{}) (string, error) {
	s, err := cast.ToStringE(in)

	if err != nil {
		return "", err
	}

	return helpers.StripHTML(s), nil
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:10,代码来源:template_funcs.go


示例18: base64Encode

// base64Encode returns the base64 encoding of the given content.
func base64Encode(content interface{}) (string, error) {
	conv, err := cast.ToStringE(content)

	if err != nil {
		return "", err
	}

	return base64.StdEncoding.EncodeToString([]byte(conv)), nil
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:10,代码来源:template_funcs.go


示例19: Param

// Param is a convenience method to do lookups in Page's and Site's Params map,
// in that order.
//
// This method is also implemented on Node.
func (p *Page) Param(key interface{}) (interface{}, error) {
	keyStr, err := cast.ToStringE(key)
	if err != nil {
		return nil, err
	}
	if val, ok := p.Params[keyStr]; ok {
		return val, nil
	}
	return p.Site.Params[keyStr], nil
}
开发者ID:JTrembl1,项目名称:hugo,代码行数:14,代码来源:page.go


示例20: Menus

func (p *Page) Menus() PageMenus {
	p.pageMenusInit.Do(func() {
		p.pageMenus = PageMenus{}

		if ms, ok := p.Params["menu"]; ok {
			link, _ := p.RelPermalink()

			me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, URL: link}

			// Could be the name of the menu to attach it to
			mname, err := cast.ToStringE(ms)

			if err == nil {
				me.Menu = mname
				p.pageMenus[mname] = &me
				return
			}

			// Could be a slice of strings
			mnames, err := cast.ToStringSliceE(ms)

			if err == nil {
				for _, mname := range mnames {
					me.Menu = mname
					p.pageMenus[mname] = &me
				}
				return
			}

			// Could be a structured menu entry
			menus, err := cast.ToStringMapE(ms)

			if err != nil {
				jww.ERROR.Printf("unable to process menus for %q\n", p.Title)
			}

			for name, menu := range menus {
				menuEntry := MenuEntry{Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name}
				if menu != nil {
					jww.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title)
					ime, err := cast.ToStringMapE(menu)
					if err != nil {
						jww.ERROR.Printf("unable to process menus for %q: %s", p.Title, err)
					}

					menuEntry.marshallMap(ime)
				}
				p.pageMenus[name] = &menuEntry

			}
		}
	})

	return p.pageMenus
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:55,代码来源:page.go



注:本文中的github.com/spf13/cast.ToStringE函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cast.ToStringMap函数代码示例发布时间:2022-05-28
下一篇:
Golang cast.ToString函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap