What is the difference between int and int64 in Go?

I have a line containing an integer (which was read from a file).

I am trying to convert string to int using strconv.ParseInt() . ParseInt requires me to provide a bitraiser (bit sizes 0, 8, 16, 32, and 64 match int, int8, int16, int32, and int64).

The integer read from the file is small (i.e., it must match a normal int). However, if I pass the bit digit 0, I get an int64 type int64 (presumably because I work on a 64-bit OS).

Why is this happening? How can I get a normal int? (If anyone has a quick example of when and why I should use different int types, that would be awesome!)

Edit: I can convert int64 to regular int using int([i64_var]) . But I still don't understand why ParseInt() gives me int64 when I request a bitrate of 0.

+43
go
Jan 31 '14 at 22:47
source share
4 answers
 func ParseInt(s string, base int, bitSize int) (i int64, err error) 

ParseInt always returns int64

bitSize defines a range of values. If the value corresponding to s cannot be represented by an integer with a sign of a given size, err.Err = ErrRange.

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

 type int int 

int is a signed integer type whose size is at least 32 bits. However, this is a special type, not an alias, for example, for int32.

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

So int may be more than 32 bits in the future or on some systems, such as int in C.

I think some int64 systems may be faster than int32 because this system only works with 64-bit integers.

Below is an example 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) } 
+36
Jan 31 '14 at 23:03
source share

Strconv package

func parseInt

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

ParseInt interprets the string s in this database (from 2 to 36) and returns the corresponding value i. If base == 0, the base is the string prefix: base 16 for "0x", base 8 for "0" and base 10 otherwise.

The bitSize argument specifies the integer type to fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

Errors returned by ParseInt are of specific 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 an integer with a sign of a given size, err.Err = ErrRange.

ParseInt always returns an int64 value. Depending on bitSize this value will correspond to int , int8 , int16 , int32 or int64 . If the value cannot be represented as an integer with the size sign specified by bitSize , then err.Err = ErrRange .

Go programming language specification

Numeric types

The value of an n-bit integer is n bits and is represented using two 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 predefined numeric types with specific implementation sizes:

 uint either 32 or 64 bits int same size as uint 

int - 32 or 64 bits, depending on the implementation. Usually these are 32 bits for 32-bit compilers and 64 bits for 64-bit compilers.

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

Strconv package

Constants

 const IntSize = intSize 

IntSize is the bit size of the 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 
+19
Jan 31 '14 at 23:04
source share

strconv.ParseInt and friends return 64-bit versions to keep the API clean and simple. Others would need to create separate versions for each possible return type. Or return interface{} , which will then have to go through a type statement. None of them are perfect.

int64 is chosen because it can contain any integer size up to 64-bit supported 64-bit inclusions. The size of the bits that you pass to the function ensures that the value is clamped correctly in the correct range. That way, you can simply convert the type to a return value to turn it into any type of integer type that you need.

As for the difference between int and int64 , it depends on the architecture. int is just an alias for a 32-bit or 64-bit integer, depending on the architecture you are compiling.

For the keen eye: the return value is a signed integer. There is a separate strconv.ParseUint function for strconv.ParseUint integers that returns uint64 and follows the same reasoning as explained above.

+5
Jan 31 '14 at 23:07
source share

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

The other answers were pretty comprehensive in explaining the int type, but I think there is a link to the Go language specification here: http://golang.org/ref/spec#Numeric_types

+4
Jan 31 '14 at 23:47
source share



All Articles