int和int64在Go语言中有什么区别?

huangapple go评论90阅读模式
英文:

What is the difference between int and int64 in Go?

问题

我有一个包含整数的字符串(从文件中读取的)。我试图使用strconv.ParseInt()string转换为intParseInt要求我提供一个位大小(位大小0、8、16、32和64对应于int、int8、int16、int32和int64)。

从文件中读取的整数很小(即它应该适合普通的int)。然而,如果我传递一个位大小为0,我得到的结果是int64类型(可能是因为我在64位操作系统上运行)。

为什么会发生这种情况?我如何只获取一个普通的int?(如果有人能给我一个关于何时以及为什么使用不同int类型的快速入门,那就太棒了!)

编辑:我可以使用int([i64_var])将int64转换为普通的int。但我仍然不明白为什么当我请求一个位大小为0时,ParseInt()会给我一个int64。

英文:

I have a string containing an integer (which has been read from a file).

I'm trying to convert the string to an int using strconv.ParseInt(). ParseInt requires that I provide a bitsize (bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64).

The integer read from the file is small (i.e. it should fit in a normal int). If I pass a bitsize of 0, however, I get a result of type int64 (presumably because I'm running on a 64-bit OS).

Why is this happening? How do I just get a normal int? (If someone has a quick primer on when and why I should use the different int types, that would awesome!)

Edit: I can convert the int64 to a normal int using int([i64_var]). But I still don't understand why ParseInt() is giving me an int64 when I'm requesting a bitsize of 0.

答案1

得分: 72

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

`ParseInt`总是返回`int64`类型

`bitSize`定义了值的范围

> 如果与`s`对应的值无法用给定大小的有符号整数表示err.Err = ErrRange

http://golang.org/pkg/strconv/#ParseInt

type int int
> `int`是一个至少有32位大小的有符号整数类型但它是一个独立的类型不是`int32`的别名

http://golang.org/pkg/builtin/#int

因此`int`在将来或某些系统上可能比32位更大就像C语言中的`int`一样

我猜在某些系统上`int64`可能比`int32`更快因为该系统只能处理64位整数

这是`bitSize`为8时出现错误的示例

http://play.golang.org/p/_osjMqL6Nj

package main

import (
	"fmt"
	"strconv"
)

func main() {
	i, err := strconv.ParseInt("123456", 10, 8)
	fmt.Println(i, err)
}
英文:
func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt always returns int64.

bitSize defines the range of values.

> If the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

http://golang.org/pkg/strconv/#ParseInt

type int int

> int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.

http://golang.org/pkg/builtin/#int

So int could be bigger than 32 bit in the future or on some systems like int in C.

I guess on some systems int64 might be faster than int32 because that system only works with 64-bit integers.

Here is an example of an error when bitSize is 8:

http://play.golang.org/p/_osjMqL6Nj

package main

import (
	"fmt"
	"strconv"
)

func main() {
	i, err := strconv.ParseInt("123456", 10, 8)
	fmt.Println(i, err)
}

答案2

得分: 34

> Package strconv

> func ParseInt

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

> ParseInt函数将给定的字符串s解析为指定进制(2到36)的整数,并返回对应的值i。如果base为0,则根据字符串的前缀来确定进制:以"0x"开头的为16进制,以"0"开头的为8进制,其他情况为10进制。

> bitSize参数指定了结果必须适应的整数类型。bitSize为0、8、16、32和64分别对应int、int8、int16、int32和int64。

> ParseInt返回的错误具有具体类型*NumError,并且包含err.Num = s。如果s为空或包含无效的数字,则err.Err = ErrSyntax;如果s对应的值无法用给定大小的有符号整数表示,则err.Err = ErrRange。

ParseInt函数总是返回一个int64值。根据bitSize的不同,该值可以适应intint8int16int32int64。如果该值无法用给定bitSize大小的有符号整数表示,则err.Err = ErrRange

> The Go Programming Language Specification

> Numeric types

> n位整数的值是n位宽,并使用二进制补码算术表示。

> int8 所有有符号的8位整数(-128到127)
> int16 所有有符号的16位整数(-32768到32767)
> int32 所有有符号的32位整数(-2147483648到2147483647)
> int64 所有有符号的64位整数(-9223372036854775808到9223372036854775807)

还有一组预声明的数值类型,其大小由实现决定:

> uint 32位或64位
> int 与uint大小相同

int的大小取决于实现,可以是32位或64位。通常情况下,32位编译器使用32位,64位编译器使用64位。

要获取intuint的大小,可以使用strconv.IntSize

> Package strconv

> Constants

> const IntSize = intSize

IntSizeintuint值的位数大小。

例如:

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
    fmt.Println(strconv.IntSize)
}

输出:

gc amd64 linux
64
英文:

> Package strconv
>
> func ParseInt
>
> func ParseInt(s string, base int, bitSize int) (i int64, err error)
>
> ParseInt interprets a string s in the given base (2 to 36) 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.
>
> 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.
>
> 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; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

ParseInt always returns an int64 value. Depending on bitSize, this value will fit into int, int8, int16, int32, or int64. If the value cannot be represented by a signed integer of the size given by bitSize, then err.Err = ErrRange.

> The Go Programming Language Specification
>
> Numeric types
>
> The value of an n-bit integer is n bits wide and represented using
> two's complement arithmetic.
>
> int8 the set of all signed 8-bit integers (-128 to 127)
> int16 the set of all signed 16-bit integers (-32768 to 32767)
> int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
> int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
> There is also a set of predeclared numeric types with
> implementation-specific sizes:
>
> uint either 32 or 64 bits
> int same size as uint

int is either 32 or 64 bits, depending on the implementation. Usually it's 32 bits for 32-bit compilers and 64 bits for 64-bit compilers.

To find out the size of an int or uint, use strconv.IntSize.

> Package strconv
>
> Constants
>
> const IntSize = intSize
>
> IntSize is the size in bits of an int or uint value.

For example,

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
    fmt.Println(strconv.IntSize)
}

Output:

gc amd64 linux
64

答案3

得分: 7

strconv.ParseInt和相关函数返回64位版本,以保持API的简洁和简单性。
否则,就必须为每种可能的返回类型创建单独的版本。或者返回interface{},然后必须进行类型断言。这些都不是理想的解决方案。

选择int64是因为它可以容纳任何整数大小,包括支持的64位。您传递给函数的位大小确保将值正确地限制在正确的范围内。因此,您可以对返回的值进行类型转换,将其转换为所需的任何整数类型。

至于intint64之间的区别,这取决于体系结构。int只是一个别名,可以是32位或64位整数,具体取决于您正在编译的体系结构。

对于敏锐的眼睛:返回的值是有符号整数。对于无符号整数,有一个单独的strconv.ParseUint函数,返回uint64,并遵循上述相同的推理。

英文:

strconv.ParseInt and friends return 64-bit versions to keep the API clean and simple.
Else one would have to create separate versions for each possible return type. Or return interface{}, which would then have to go through a type assertion. None of which are ideal.

int64 is chosen, because it can hold any integer size up to, and including, the supported 64-bits. The bit size you pass into the function, ensures that the value is properly clamped to the correct range. So you can simply do a type conversion on the returned value, to turn it into whatever integer type you require.

As for the difference between int and int64, this is architecture-dependant. int is simply an alias for either a 32-bit or 64-bit integer, depending on the architecture you are compiling for.

For the discerning eye: The returned value is a signed integer. There is a separate strconv.ParseUint function for unsigned integers, which returns uint64 and follows the same reasoning as explained above.

答案4

得分: 6

对于你的目的,strconv.Atoi()可能更方便。

其他的回答已经非常详尽地解释了int类型,但我认为在这里提供Go语言规范的链接是有必要的:http://golang.org/ref/spec#Numeric_types

英文:

For your purposes, strconv.Atoi() would be more convenient I think.

The other answers have been pretty exhaustive about explaining the int type, but I think a link to the Go language specification is merited here: http://golang.org/ref/spec#Numeric_types

答案5

得分: 3

一个int是Go语言中的默认有符号类型:在32位机器上占用32位(4字节),在64位机器上占用64位(8字节)。
参考资料- 《The way to go》 by Ivo Balbaert

英文:

An int is the default signed type in go: it takes 32 bits (4 bytes) on a 32-bit machine and 64 bits(8 bytes) on a 64-bit machine.
Reference- The way to go by Ivo Balbaert

答案6

得分: 1

在Go语言中,每种类型都被视为独立的数据类型,不能与基本类型互换使用。例如,

type CustomInt64 int64

在上述声明中,CustomInt64和内置的int64是两种不同的数据类型,不能互换使用。

对于int、int32和int64也是如此,它们都是独立的数据类型,不能互换使用。其中,int32是32位的整数类型,int64是64位的整数类型,而**通用int类型的大小取决于平台。在32位系统上,它是32位宽度,在64位系统上,它是64位宽度。**因此,在指定通用数据类型如int、uint和float时,我们必须小心并明确。否则,在代码的某个地方可能会出现问题,并且在不同的平台上会导致应用程序崩溃。

英文:

In Go lang, each type is considered as separate data type which can not be used interchangeably with the base type. For example,

type CustomInt64 int64

In the above declaration, CustomInt64 and built-in int64 are two separate data types and cannot be used interchangeably.

The same is the case with int, int32, and int64, all of these are separate data types that can't be used interchangeably. Where int32 is 32 its integer type, int64 is 64 bits and the size of the generic int type is platform dependent. It is 32 bits wide on a 32-bit system and 64-bits wide on a 64-bit system. So we must be careful and specific while specifying generic data types like int, uint, and float. It may cause a problem somewhere in code and will crash application on a different platform.

huangapple
  • 本文由 发表于 2014年2月1日 06:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/21491488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定