gpt4 book ai didi

go - 尝试生成唯一的int64时生成负数的代码

转载 作者:行者123 更新时间:2023-12-01 22:44:32 24 4
gpt4 key购买 nike

我正在尝试使用下面的Go代码创建唯一的int64

unixtimestamp := time.Now().UnixNano()
node := AppConfig.NodeID << 44
timestamp := unixtimestamp << 12
var sequenceID int64
for sequenceID = 1; sequenceID <= count; sequenceID++ {
id := node | timestamp | sequenceID
idList = append(idList, id)
}

上面的代码也生成负数。我如何解决它?

最佳答案

检查是否为负使其为正:

unixtimestamp := time.Now().UnixNano()
node := AppConfig.NodeID << 44
timestamp := unixtimestamp << 12
var sequenceID int64
for sequenceID = 1; sequenceID <= count; sequenceID++ {
id := node | timestamp | sequenceID
if id < 0 {
id = id * -1
}
idList = append(idList, id)
}

关于go - 尝试生成唯一的int64时生成负数的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60601887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com