Program Tip

Go에서 int와 int64의 차이점은 무엇입니까?

programtip 2020. 10. 27. 23:06
반응형

Go에서 int와 int64의 차이점은 무엇입니까?


정수가 포함 된 문자열이 있습니다 (파일에서 읽음).

나는 변환하기 위해 노력하고있어 stringint사용 strconv.ParseInt(). ParseInt비트 크기를 제공해야합니다 (비트 크기 0, 8, 16, 32 및 64는 int, int8, int16, int32 및 int64에 해당).

파일에서 읽은 정수는 작습니다 (즉, 일반 정수에 맞아야합니다). 그러나 비트 크기 0을 전달하면 유형의 결과가 표시됩니다 int64(아마도 64 비트 OS에서 실행 중이기 때문에).

왜 이런 일이 발생합니까? 일반 정수를 어떻게 얻습니까? (누군가가 다른 int 유형을 사용해야하는시기와 이유에 대한 빠른 입문서를 가지고 있다면 멋질 것입니다!)

편집 : .NET을 사용하여 int64를 일반 int로 변환 할 수 있습니다 int([i64_var]). 그러나 나는 여전히 ParseInt()비트 크기 0을 요청할 때 int64를 제공 하는지 이해하지 못합니다 .


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향후에 또는 intC 와 같은 일부 시스템에서 32 비트보다 클 수 있습니다 .

일부 시스템 에서는 해당 시스템이 64 비트 정수로만 작동하기 때문에 int64더 빠를 수 있습니다 int32.

다음은 bitSize8 일 때 오류의 예입니다.

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

package main

import (
    "fmt"
    "strconv"
)

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

패키지 strconv

func ParseInt

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

ParseInt는 주어진 밑 (2 ~ 36)의 문자열 s를 해석하고 해당 값 i를 반환합니다. base == 0이면 base는 문자열의 접두사로 내포됩니다. "0x"의 경우 base 16, "0"의 경우 base 8, 그렇지 않으면 base 10입니다.

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,이 값에 맞는 int, int8, int16, int32, 또는 int64. 값에 의해 주어진 크기의 부호있는 정수로 표현 할 수없는 경우 bitSize다음 err.Err = ErrRange.

Go 프로그래밍 언어 사양

숫자 유형

n 비트 정수의 값은 n 비트 폭이며 2의 보수 산술을 사용하여 표현됩니다.

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)

구현 별 크기가있는 사전 선언 된 숫자 유형 세트도 있습니다.

uint     either 32 or 64 bits
int      same size as uint

int구현에 따라 32 비트 또는 64 비트입니다. 일반적으로 32 비트 컴파일러의 경우 32 비트이고 64 비트 컴파일러의 경우 64 비트입니다.

int또는 의 크기를 확인하려면을 uint사용하십시오 strconv.IntSize.

패키지 strconv

상수

const IntSize = intSize

IntSizeint또는 uint값의 비트 단위 크기입니다 .

예를 들면

package main

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

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

산출:

gc amd64 linux
64

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.


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

참고URL : https://stackoverflow.com/questions/21491488/what-is-the-difference-between-int-and-int64-in-go

반응형