cloudwego / cloudwego.github.io

Website for CloudWeGo
https://www.cloudwego.io
Apache License 2.0
126 stars 239 forks source link

基础示例int48.go:25,不兼容32位系统。 #1087

Open afjun opened 1 month ago

afjun commented 1 month ago

在初步使用kitex的时候,我仿照官方的文档,在hello目录下执行go run .。但是报错信息如下:

C:\Users\Administrator\go\pkg\mod\github.com\cloudwego\dynamicgo@v0.2.0\internal\rt\int48.go:25:25: MinInt48 (untyped int constant -140737488355328) overflows int

报错的代码部分如下:

/*
 * Copyright 2023 CloudWeGo Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package rt

const (
    MinInt48 = -(1 << 47)
    MaxInt48 = +(1 << 47) - 1
)

func PackInt(v int) uint64 {
    if u := uint64(v); v < MinInt48 || v > MaxInt48 {
        panic("int48 out of range")
    } else {
        return ((u >> 63) << 47) | (u & 0x00007fffffffffff)
    }
}

func UnpackInt(v uint64) int {
    v &= 0x0000ffffffffffff
    v |= (v >> 47) * (0xffff << 48)
    return int(v)
}

报错代码行为第25行的MinInt48。这是一个48位整数,但是报错数字越界。此外,MinInt48v进行了大小比较,说明这两个变量类型是相同的。在Golang中,int类型由平台确定。建议对32位兼容。

rogerogers commented 1 month ago

32 位现在系统都很少支持了吧