Compare commits

..

No commits in common. "master" and "v1.3.0" have entirely different histories.

5 changed files with 26 additions and 19 deletions

View File

@ -4,7 +4,6 @@ env:
sudo: required
go:
- "1.11.x"
- "1.12.x"
- tip
os:
- linux

View File

@ -1,4 +1,4 @@
GOVERSION := $(shell go version | cut -d ' ' -f 3 | cut -d '.' -f 2)
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: check fmt lint test test-race vet test-cover-html help
.DEFAULT_GOAL := help
@ -12,13 +12,11 @@ test-race: ## Run tests with race detector
go test -race ./...
fmt: ## Run gofmt linter
ifeq "$(GOVERSION)" "12"
@for d in `go list` ; do \
if [ "`gofmt -l -s $$GOPATH/src/$$d | tee /dev/stderr`" ]; then \
echo "^ improperly formatted go files" && echo && exit 1; \
fi \
done
endif
lint: ## Run golint linter
@for d in `go list` ; do \

View File

@ -4,25 +4,35 @@ cast
[![Build Status](https://api.travis-ci.org/spf13/cast.svg?branch=master)](https://travis-ci.org/spf13/cast)
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast)
在Go中轻松安全地从一种类型转换为另一种类型
Easy and safe casting from one type to another in Go
Dont Panic! ... Cast
## What is Cast?
Cast是一个库用于以一致且简单的方式在不同的go类型之间进行转换。
Cast is a library to convert between different go types in a consistent and easy way.
Cast提供简单的功能可轻松将数字转换为字符串进入布尔等接口。当明显转换是可能的。 它不会尝试猜测您的意思例如您只能在字符串为字符串时将其转换为int
int的表示形式例如“ 8”。 Cast的开发目的是用于[Hugo](http://hugo.spf13.com), 使用YAMLTOML或JSON的网站引擎用于元数据。
Cast provides simple functions to easily convert a number to a string, an
interface into a bool, etc. Cast does this intelligently when an obvious
conversion is possible. It doesnt make any attempts to guess what you meant,
for example you can only convert a string to an int when it is a string
representation of an int such as “8”. Cast was developed for use in
[Hugo](http://hugo.spf13.com), a website engine which uses YAML, TOML or JSON
for meta data.
## Why use Cast?
在Go中使用动态数据时您通常需要强制转换或转换数据
从一种类型变成另一种类型。 强制转换不仅限于使用类型断言(尽管
它在可能的情况下使用它)来提供非常直接和方便的库。
When working with dynamic data in Go you often need to cast or convert the data
from one type into another. Cast goes beyond just using type assertion (though
it uses that when possible) to provide a very straightforward and convenient
library.
如果您正在使用接口来处理诸如动态内容之类的内容您将需要一种简单的方法来将接口转换为给定类型。 这个是您的库。
如果您要从YAMLTOML或JSON或其他缺少格式的数据中获取数据完整类型那么Cast是适合您的库。
If you are working with interfaces to handle things like dynamic content
youll need an easy way to convert an interface into a given type. This
is the library for you.
If you are taking in data from YAML, TOML or JSON or other formats which lack
full types, then Cast is the library for you.
## Usage

View File

@ -819,15 +819,15 @@ func ToStringE(i interface{}) (string, error) {
case int8:
return strconv.FormatInt(int64(s), 10), nil
case uint:
return strconv.FormatUint(uint64(s), 10), nil
return strconv.FormatInt(int64(s), 10), nil
case uint64:
return strconv.FormatUint(uint64(s), 10), nil
return strconv.FormatInt(int64(s), 10), nil
case uint32:
return strconv.FormatUint(uint64(s), 10), nil
return strconv.FormatInt(int64(s), 10), nil
case uint16:
return strconv.FormatUint(uint64(s), 10), nil
return strconv.FormatInt(int64(s), 10), nil
case uint8:
return strconv.FormatUint(uint64(s), 10), nil
return strconv.FormatInt(int64(s), 10), nil
case []byte:
return string(s), nil
case template.HTML:

2
go.mod
View File

@ -1,4 +1,4 @@
module golib.gaore.com/GaoreGo/cast
module github.com/spf13/cast
require (
github.com/davecgh/go-spew v1.1.1 // indirect