如何指定unsigned整数类型可表示的最大值?

我想知道如何在下面的循环中初始化min,该循环从一些 struct 迭代计算最小和最大长度.

var minLen uint = ???
var maxLen uint = 0
for _, thing := range sliceOfThings {
  if minLen > thing.n { minLen = thing.n }
  if maxLen < thing.n { maxLen = thing.n }
}
if minLen > maxLen {
  // If there are no values, clamp min at 0 so that min <= max.
  minLen = 0
}

所以第一次通过比较,是minLen >= n.

推荐答案

https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1

最贴切的部分是:

由于整数类型使用2的补码算法,因此可以推断

const MaxUint = ^uint(0) 
const MinUint = 0 
const MaxInt = int(MaxUint >> 1) 
const MinInt = -MaxInt - 1

根据@CarelZA的 comments :

uint8  : 0 to 255 
uint16 : 0 to 65535 
uint32 : 0 to 4294967295 
uint64 : 0 to 18446744073709551615 
int8   : -128 to 127 
int16  : -32768 to 32767 
int32  : -2147483648 to 2147483647 
int64  : -9223372036854775808 to 9223372036854775807

Go相关问答推荐

如何使用go从map.dbf map.prj map.shp map.shx中的任何文件中了解层名称

如何使用GRPC UnaryClientInterceptor中的`maily`参数?

Term~T中的类型不能是类型参数,但可以是引用类型参数的切片

CGO如何转换为文件*类型

map 中的多个函数类型,Golang

有没有办法通过Go cmdline或IDE(IntelliJ)找出我的 struct 实现了什么接口?

Golang中的泛型 struct /接口列表

Cypher 查找(多个)最低 node

在运行时更改 Go lang slog 的日志(log)级别

我怎样才能改进这个嵌套逻辑以使其正常工作并提高性能

Go 中将 int 切片转换为自定义 int 切片指针类型的函数

go-jwt 令牌验证错误 - 令牌签名无效:密钥类型无效

使用Goldmark在golang中添加ChildNode会导致堆栈溢出

在 Go sync.Map 中,为什么这部分实现不一致或者我误解了什么?

如何使用 go-git 将特定分支推送到远程

如何将一片 map 转换为一片具有不同属性的 struct

如何编写一个以字符串或错误为参数的通用函数?

为什么此代码在运行命令 error="exec: not started" 时出现错误?

使用 Go 读取 TOML 文件时结果为空

Go:如何通过 GIN-Router 从 AWS S3 将文件作为二进制流发送到浏览器?