Giter Site home page Giter Site logo

cast's Introduction

cast

GitHub Workflow Status PkgGoDev Go Version Go Report Card

Easy and safe casting from one type to another in Go

Don’t Panic! ... Cast

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

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 doesn’t 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, a website engine which uses YAML, TOML or JSON for meta data.

Why use Cast?

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.

If you are working with interfaces to handle things like dynamic content you’ll 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

Cast provides a handful of To_____ methods. These methods will always return the desired type. If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

Cast also provides identical methods To_____E. These return the same result as the To_____ methods, plus an additional error which tells you if it successfully converted. Using these methods you can tell the difference between when the input matched the zero value or when the conversion failed and the zero value was returned.

The following examples are merely a sample of what is available. Please review the code for a complete set.

Example ‘ToString’:

cast.ToString("mayonegg")         // "mayonegg"
cast.ToString(8)                  // "8"
cast.ToString(8.31)               // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil)                // ""

var foo interface{} = "one more time"
cast.ToString(foo)                // "one more time"

Example ‘ToInt’:

cast.ToInt(8)                  // 8
cast.ToInt(8.31)               // 8
cast.ToInt("8")                // 8
cast.ToInt(true)               // 1
cast.ToInt(false)              // 0

var eight interface{} = 8
cast.ToInt(eight)              // 8
cast.ToInt(nil)                // 0

cast's People

Contributors

asdine avatar bep avatar chancez avatar chyeh avatar cognusion avatar dependabot[bot] avatar derekperkins avatar diggs avatar elwinar avatar gowithrain avatar heewa avatar jackspirou avatar jmhodges avatar k4rian avatar moorereason avatar pacodw avatar qwxingzhe avatar raehik avatar rvolosatovs avatar sagikazarmark avatar schumacherfm avatar sevennt avatar spf13 avatar theodesp avatar tuziemon avatar txgruppi avatar xvello avatar yveshield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cast's Issues

Tests are not passed

 git clone https://github.com/spf13/cast && cd cast && go test .
go: downloading github.com/stretchr/testify v1.2.2
--- FAIL: TestToTimeWithTimezones (0.02s)
    --- FAIL: TestToTimeWithTimezones/3;timeFormatType=1;Mon,_02_Jan_2006_15:04:05_MST (0.00s)
        --- FAIL: TestToTimeWithTimezones/3;timeFormatType=1;Mon,_02_Jan_2006_15:04:05_MST/local_timezone_without_a_default_location (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1358
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0000"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0000"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0000
                	            	+Fri, 01 Jan 2016 01:00:00 +0000
                	Test:       	TestToTimeWithTimezones/3;timeFormatType=1;Mon,_02_Jan_2006_15:04:05_MST/local_timezone_without_a_default_location
        --- FAIL: TestToTimeWithTimezones/3;timeFormatType=1;Mon,_02_Jan_2006_15:04:05_MST/time_in_the_local_timezone_default_location_not_UTC (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1411
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0330"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0330"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0330
                	            	+Fri, 01 Jan 2016 01:00:00 +0330
                	Test:       	TestToTimeWithTimezones/3;timeFormatType=1;Mon,_02_Jan_2006_15:04:05_MST/time_in_the_local_timezone_default_location_not_UTC
    --- FAIL: TestToTimeWithTimezones/5;timeFormatType=1;02_Jan_06_15:04_MST (0.00s)
        --- FAIL: TestToTimeWithTimezones/5;timeFormatType=1;02_Jan_06_15:04_MST/local_timezone_without_a_default_location (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1358
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0000"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0000"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0000
                	            	+Fri, 01 Jan 2016 01:00:00 +0000
                	Test:       	TestToTimeWithTimezones/5;timeFormatType=1;02_Jan_06_15:04_MST/local_timezone_without_a_default_location
        --- FAIL: TestToTimeWithTimezones/5;timeFormatType=1;02_Jan_06_15:04_MST/time_in_the_local_timezone_default_location_not_UTC (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1411
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0330"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0330"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0330
                	            	+Fri, 01 Jan 2016 01:00:00 +0330
                	Test:       	TestToTimeWithTimezones/5;timeFormatType=1;02_Jan_06_15:04_MST/time_in_the_local_timezone_default_location_not_UTC
    --- FAIL: TestToTimeWithTimezones/6;timeFormatType=1;Monday,_02-Jan-06_15:04:05_MST (0.00s)
        --- FAIL: TestToTimeWithTimezones/6;timeFormatType=1;Monday,_02-Jan-06_15:04:05_MST/local_timezone_without_a_default_location (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1358
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0000"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0000"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0000
                	            	+Fri, 01 Jan 2016 01:00:00 +0000
                	Test:       	TestToTimeWithTimezones/6;timeFormatType=1;Monday,_02-Jan-06_15:04:05_MST/local_timezone_without_a_default_location
        --- FAIL: TestToTimeWithTimezones/6;timeFormatType=1;Monday,_02-Jan-06_15:04:05_MST/time_in_the_local_timezone_default_location_not_UTC (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1411
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0330"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0330"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0330
                	            	+Fri, 01 Jan 2016 01:00:00 +0330
                	Test:       	TestToTimeWithTimezones/6;timeFormatType=1;Monday,_02-Jan-06_15:04:05_MST/time_in_the_local_timezone_default_location_not_UTC
    --- FAIL: TestToTimeWithTimezones/12;timeFormatType=1;Mon_Jan__2_15:04:05_MST_2006 (0.00s)
        --- FAIL: TestToTimeWithTimezones/12;timeFormatType=1;Mon_Jan__2_15:04:05_MST_2006/local_timezone_without_a_default_location (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1358
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0000"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0000"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0000
                	            	+Fri, 01 Jan 2016 01:00:00 +0000
                	Test:       	TestToTimeWithTimezones/12;timeFormatType=1;Mon_Jan__2_15:04:05_MST_2006/local_timezone_without_a_default_location
        --- FAIL: TestToTimeWithTimezones/12;timeFormatType=1;Mon_Jan__2_15:04:05_MST_2006/time_in_the_local_timezone_default_location_not_UTC (0.00s)
            require.go:157:
                	Error Trace:	cast_test.go:1428
                	            				cast_test.go:1411
                	Error:      	Not equal:
                	            	expected: "Fri, 01 Jan 2016 00:00:00 +0330"
                	            	actual  : "Fri, 01 Jan 2016 01:00:00 +0330"

                	            	Diff:
                	            	--- Expected
                	            	+++ Actual
                	            	@@ -1 +1 @@
                	            	-Fri, 01 Jan 2016 00:00:00 +0330
                	            	+Fri, 01 Jan 2016 01:00:00 +0330
                	Test:       	TestToTimeWithTimezones/12;timeFormatType=1;Mon_Jan__2_15:04:05_MST_2006/time_in_the_local_timezone_default_location_not_UTC
FAIL
FAIL	github.com/spf13/cast	0.261s
FAIL

Add helper functions for checking data types

I like your project a lot. Although I know this project is dedicated to cast variables from one data type to another, it would be nice to have some helper functions to simplify the checking of a variable's data type such as the following function:

func IsArray(v interface{}) bool {
	rv := reflect.ValueOf(v)
	return rv.Kind() == reflect.Array || rv.Kind() == reflect.Slice
}

"ToStringSlice" does not support slice of ints?

I was hoping that ToStringSlice would support turning a slice of ints into a slice of strings. Am I wrong in that assumption?

The following fails:

nums := []int{1,2,3,4,5}
out := cast.ToStringSlice(nums)
// expected: []string{"1","2","3","4","5"}
// actual: []

If I use ToStringSliceE I get the error:

unable to cast []int{1, 2, 3, 4, 5} of type []int to []string

Is there a cast method that does what I want that I'm not aware of?

Support inline json for []string

Hi,
It's a bit confusing to use inline json for map but having to use space separated items for slices.
For consistency it would be great to also support inline JSON for list.
What do you think?

Begin using SemVer release tags

Proposal

Start tagging releases, beginning with v1.0.0. Cast is stable. We have 100% test coverage with no known defects.

Rationale

As a library, tagging stable releases communicates stability to the user (both in the code base and the API). We should strive to be good citizens of the Go community and help foster a mature ecosystem.

The forthcoming Go dep tool will use SemVer (via Masterminds/semver). Dave Cheney argues two things: tagging releases is a prerequisite to a better ecosystem ("No version information, no tools, and the situation never improves"); and the tag should start with a "v". I care less about the "v", but I prefer to have it.

Tagging releases would also allow us to make larger changes. I'm specifically thinking about spf13/afero #89, which would probably necessitate a new major version.

Aspirations

If we agree that this is a good idea, we should extend it to the other spf13 libraries (afero, cobra, viper, etc). Cast is a tiny library, so I figure we can start here and work our way up.

Add overflow check

You clam your library to be "safe casting from one type to another in Go", but in the code there are no checks for overflow when casting integer types:

switch s := i.(type) {
	case int:
		return int16(s), nil  // <-- this may overflow
	case int64:
		return int16(s), nil  // <-- this may overflow
	case int32:
		return int16(s), nil
	case int16:
		return s, nil
	case int8:
		return int16(s), nil
	case uint:
		return int16(s), nil   // <-- this may overflow
	case uint64:
		return int16(s), nil   // <-- this may overflow
	case uint32:
		return int16(s), nil   // <-- this may overflow
	case uint16:
		return int16(s), nil   // <-- this may overflow
	case uint8:
		return int16(s), nil
	case float64:
		return int16(s), nil   // <-- this may overflow
	case float32:
		return int16(s), nil  // <-- this may overflow

Adding bounds check like this: https://play.golang.org/p/Jgu2NYg1qi3 would solve the issue.

Return error result when transform digital type (except int) to bool

As title said, when you transform int16, int64 or other digital type (not int) to bool, return an error result.

func try()  {
	var i int64 = 1
	fmt.Println(cast.ToBool(i)) // false
}

because cast only assert int type.

// ToBoolE casts an interface to a bool type.
func ToBoolE(i interface{}) (bool, error) {
	i = indirect(i)

	switch b := i.(type) {
	case bool:
		return b, nil
	case nil:
		return false, nil
	case int:
		if i.(int) != 0 {
			return true, nil
		}
		return false, nil
	case string:
		return strconv.ParseBool(i.(string))
	default:
		return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
	}
}

Bug: Wrong conversion ToUint64

...
inValue = "18446744073709551615"
log.Printf("inValue = %v", inValue )
outValue := cast.ToUint64(inValue )
log.Printf("outValue = %v", outValue )
...

Result for v1.4.1:

INFO[2022-06-06T14:01:00+03:00] inValue = 18446744073709551615               
INFO[2022-06-06T14:01:00+03:00] outValue = 18446744073709551615

Result for v1.5.0:

INFO[2022-06-06T14:01:00+03:00] inValue = 18446744073709551615               
INFO[2022-06-06T14:01:00+03:00] outValue = 0

not safe for uint64

I have a value id=16607535815717977617:

id1 := cast.ToString(id)            // -1839208257991573999
id2 := strconv.FormatUint(id, 10)   // 16607535815717977617

which is an unexpected behavior.

StringToDate doesn't parse all common RFC 3339 strings

Redirected from Hugo issue gohugoio/hugo#5340 .

2006-01-02T15:04:05-0700 is a valid RFC 3339 date which is not successfully parsed by cast. Obviously we shouldn't be expected to parse every single date format on the planet, or even all the various RFC 3339 formats. strftime however can only output its timezone in ±hhmm format, so the omitted colon form is relatively common.

It's a trivial change, so I may make an associated PR.

cast.ToString: Fails to convert types based on string.

Here's an example:

package main

import (
    "fmt"
    "github.com/spf13/cast"
)

type BusinessStatusEnum string

const (
    BusinessStatusInactive BusinessStatus = "Inactive"
    BusinessStatusInvited  BusinessStatus = "Invited"
    BusinessStatusActive   BusinessStatus = "Active"
    BusinessStatusBlocked  BusinessStatus = "Blocked"
)

func main() {
    fmt.Printf("message should be(%s), but cast returns (%s)\n", BusinessStatusInactive, cast.ToString(BusinessStatusInactive))
}

Running the above program outputs:

jeff_walter@martai:~/Desktop/mart.ai/src/github.com/martai/platform$ !g
go run blah.go 
message should be(Inactive), but cast returns ()

I would expect it to print the associated string value, Inactive in this case.

Add uint, uint64 support...

Hi guys, you need to add uint cast support.
Now I have an error :(

cast.ToInt64E(uint(42))
//Unable to Cast 0x2a to int64

'cast.ToString()' conversion error

A strange error occurs when I use toString. The example code is as follows:

package main

import (
	"fmt"
	"github.com/spf13/cast"
)
type NewString string

func main() {
	var test NewString
	test = "this is a test string"
	fmt.Println("original: ",test)
	fmt.Println("use string() func: ",string(test))
	fmt.Println("use cast.ToString() func: ",cast.ToString(test))
	fmt.Println(cast.ToString(test)=="")
}

Output:

original:  this is a test string
use string() func:  this is a test string
use cast.ToString() func:  
true

bug of toInt toInt32 toInt64

a := []string{"01", "02", "03", "04", "05", "06", "07", "08", "09"}
for _, v := range a {
fmt.Println(cast.ToInt64(v))
}
// output 1 2 3 4 5 6 7 0 0
// bug 08 => 0 09 =>0

FeatureRequest: `ToBool` add `int*` support

cast/caste.go

Lines 81 to 99 in 8807572

func ToBoolE(i interface{}) (bool, error) {
i = indirect(i)
switch b := i.(type) {
case bool:
return b, nil
case nil:
return false, nil
case int:
if i.(int) != 0 {
return true, nil
}
return false, nil
case string:
return strconv.ParseBool(i.(string))
default:
return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
}
}

there only assert type int, no int8, int16, int32, int64, uint ...

could we add these type case?

Cast to reflect.Value

This would be pretty nice building block for reflect type casting; something like:

cast.ToReflectValue(i interface{}, value reflect.Values) error

Wrap a common "cast error" for To___E functions

To make it easier to use Go1.13's errors.Is and errors.As, it'd be great if the To___E functions wrap a common "cast error" so that transitive callers to these functions can determine if an error comes from casting.

A slightly-silly example usecase:

func main() {
  val, err := maybeCast("not_an_int")
  if errors.Is(err, cast.Err) {
    fmt.Println("unable to cast!")
  }
  fmt.Println(val)
}

func maybeCast(myVal string) (int, error) {
  if someCondition() {
    return cast.ToIntE(myVal)
  }
  return fmt.Errorf("unrelated error")
}

If there's interest, I'd love to make a PR. :)

cast.toTime(timeStr).Unix() +8

use cast.toTime(timeStr).Unix() will get +8 Hours Unix TIme,different with time.ParseInLocation.Unix(),
in Beijing Area.
How To Resolve?

Casting a string to int with a 0 in prefix does not work as expected

When I cast a string with prefixed 0 like "0123" with cast.ToInt("0123"), it results into 83 instead of the expected 123

Example code:

package main

import (
	"fmt"
	"github.com/spf13/cast"
)

func main() {
	numb := "0123"
	castedNumb := cast.ToInt(t)
	fmt.Println(numb, castedNumb)
}

Awesome!

Beautiful library, thank you!

ToIntSlice, ToBoolSlice...

Hey,

Is there any specific reason you didn't want to implement ToIntSlice, ToBoolSlice, etc...?

I'm gonna need those eventually, so, I can make a pull request if you'd like?

[BUG] Unixtimestemp in string

panic: 2> unable to parse date: 1600285405

Code for reproduction:

https://play.golang.org/p/yyxxFcFhQg1

package main

import (
	"fmt"
	cast "github.com/spf13/cast"
)

func main() {
	val, err := cast.ToTimeE(1600285405)
	if err != nil {
		panic(fmt.Sprintf("1> %v", err))
	}
	fmt.Println(val)

	val, err = cast.ToTimeE(fmt.Sprintf("%v", 1600285405))
	fmt.Println(val)

	if err != nil {
		panic(fmt.Sprintf("2> %v", err))
	}
}

Add map of int64

Could you please add
ToMapInt = map[string]int
ToMapInt64 = map[string]int64

Thank you!

missing parse on format like "2016-03-06 15:28:01 +0900"

When I tried to parse "2016-03-06 15:28:01 +0900", I got a wrong result.

code:
ToTimeE("2016-03-06 15:28:01 +0900")

result I get:
time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC)

result I want:
time.Date(2016, 3, 6, 6, 28, 1, 0, time.UTC)

ToSliceE: unable to cast []somestruct to []interface{}

Playground: https://play.golang.org/p/5bbxzoB4jNw

package main

import (
	"fmt"
	
	"github.com/spf13/cast"
)

func main() {
	b := []B{{Foo: "one"},{Foo: "two"}}
	
	bees, err := cast.ToSliceE(b)
	if err != nil {
		panic(err)
	}
	
	fmt.Printf("bees: %+v\n", bees)
}


type B struct {
	Foo string
}
panic: unable to cast []main.B{main.B{Foo:"one"}, main.B{Foo:"two"}} of type []main.B to []interface{}

goroutine 1 [running]:
main.main()
	/tmp/sandbox460370487/prog.go:14 +0x157

Program exited: status 2.

Support for ToSliceMapStringString in cast useful or better implement myself?

Hey,

thanks to cast. I found this library through viper.
I use viper to read a JSON configuration.
It works great so far. Viper has various support functions to get pre casted values. See Getting Values From Viper.

Next to standard types like string or bool is has support for GetStringMapStringSlice (which falls back to this library here).

Now my configuration file looks like this:

{
    "repositories": [
      {
        "name": "myvendor/package",
        "url": "git@othervcs:myvendor/package.git"
      }
    ],
    "dir": "/var/foo",
    "mirror": true,
    ...
}

The repositories would be a []map[string]string.
I need to do a few operations on this construct like "Is this repository is configured? If yes, is there a url?". To solve this question i do it like

func GetRepositoryURLOfPackage(n string) (*url.URL, error) {
	repositories := viper.Get("repositories")

	repositoriesSlice := repositories.([]interface{})
	if (len(repositoriesSlice) == 0) {
		return nil, errors.New("No repositories configured.")
	}

	for _, repoEntry := range repositoriesSlice {
		repoEntryMap := repoEntry.(map[string]interface{})
		if val, ok := repoEntryMap["name"]; !ok {
			if val.(string) == n {
				// TODO: Check if key "url" exists
				return repoEntryMap["url"].(string)
			}
		}
	}

	return nil, fmt.Errorf("No repository url found for package %s", n)
}

It works. A better solution would be to implement a ToStringMapStringSlice function.

Now the question: Do you see the need or does it make sense to implement a ToStringMapStringSlice into cast and enable this in viper as well?
Or should i implement it in my code and leave it there as a custom impl.?

The reason i ask:
There are multiple other usecases where those data structures are not implemented by cast like GetIntMapStringSlice, etc.
So it would be "never" complete.

Add ToStringMapStringSlice Method

I would like to have a ToStringMapStringSlice method that would return a map[string][]string type. I tend to have types like these in config files, maybe others do as well? Very helpful for YAML.

toInt/toInt64 is wrong

var value = "00100"
fmt.Println(strconv.ParseInt(value, 10, 64))
fmt.Println(strconv.Atoi(value))
fmt.Println(cast.ToInt64E(value))
fmt.Println(cast.ToIntE(value))

output

100 <nil> ✅
100 <nil> ✅
64 <nil> ❌
64 <nil>
var value = "012345678"
fmt.Println(strconv.ParseInt(value, 10, 64))
fmt.Println(strconv.Atoi(value))
fmt.Println(cast.ToInt64E(value))
fmt.Println(cast.ToIntE(value))

output

12345678 <nil> ✅
12345678 <nil> ✅
0 unable to cast "012345678" of type string to int64 ❌
0 unable to cast "012345678" of type string to int ❌

Enable Travis

It is already configure, but the repo owner must set the "switch".

Question: Why not use indirect for map

package main

import (
	"fmt"
	"github.com/spf13/cast"
)

func main() {
	a := map[string]string{"hello": "123"}

	r := cast.ToStringMapInt(a)
	fmt.Printf("%T %#v\n", r, r)

	r = cast.ToStringMapInt(&a)
	fmt.Printf("%T %#v\n", r, r)
}

//OUTPUT:
//map[string]int map[string]int{"hello":123}
//map[string]int map[string]int{}

If ToStringMapInt does indirect as well, then the ToStringMapInt method will have a friendlier result.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.