ParseInt无法转换为所需的类型

ParseInt无法转换为所需的类型

问题描述:

Here's my code:

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {
   i, _ := strconv.ParseInt("10", 10, 8)
   fmt.Println(reflect.TypeOf(i))
}

I expect i to be 8-bits long (the 3rd argument to strconv.ParseInt). It is int64, however (and the docs state that strconv.ParseInt will return int64).

What's the point of ParseInt if it always returns int64 (why not just use Atoi?)

这是我的代码: p>

包主体 p> \ n

  import(
“ fmt” 
“ reflect” 
“ strconv” 
)
 
func main(){
i,_:= strconv.ParseInt(“ 10”,10  ,8)
 fmt.Println(reflect.TypeOf(i))
} 
  code>  pre> 
 
 

我希望 i code>为8- 位长( strconv.ParseInt code>的第三个参数)。 但是它是int64(并且文档指出 strconv.ParseInt code>将返回int64)。 p>

如果ParseInt总是返回int64,那又有什么意义(为什么不返回) 只是使用Atoi吗?) p> div>

Note this from the function's doc:

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. For a bitSize below 0 or above 64 an error is returned.

So it's guaranteed that you can convert your result to a byte with byte(i).

Go doesn't have generics yet, so having a single ParseInt that can accept pointers to multiple integer types is difficult. Instead the guarantee is done with the bitSize argument

Package strconv

import "strconv"

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.

If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise. For bases 1, below 0 or above 36 an error is returned.

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. For a bitSize below 0 or above 64 an error is returned.

The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.


For example,

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i64, err := strconv.ParseInt("10", 10, 8)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%[1]d %[1]T
", i64)
    i8 := int8(i64)
    fmt.Printf("%[1]d %[1]T
", i8)

}

Playground: https://play.golang.org/p/HSHtUnC7qql

Output:

10 int64
10 int8

In Go, we often use functions to hide implementation details.

For example,

package main

import (
    "fmt"
    "strconv"
)

func ParseInt8(s string, base int) (int8, error) {
    i64, err := strconv.ParseInt(s, base, 8)
    return int8(i64), err
}

func main() {
    i8, err := ParseInt8("10", 10)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%[1]d %[1]T
", i8)

}

Playground: https://play.golang.org/p/HdA3O71U54z

Output:

10 int8

I think what you are really asking is what is the point of the 3rd parameter to ParseInt().

It's to save you having to check for overflow manually like this:

i, err := strconv.Atoi(intString)
if err != nil || i < -128 || i > 127 {
  // handle error
}
i8 := int8(i)