Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
使用int位数判断特定类型的数据,例如端口
// "28080:80"
ports := strings.Split(s, ":")
p, err := strconv.ParseUint(ports[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid port %v", ports[0])
}
port = uint16(p)
if len(ports) == 2 {
if isNodePort {
if port < 20000 || p > 40000 {
return nil, fmt.Errorf("Invalid value: %d: provided port is not in the valid range. The range of valid ports is 20000-40000", port)
}
}
p, err := strconv.ParseUint(ports[1], 10, 16)
if err != nil {
panic(fmt.Errorf("invalid target port %v", ports[1]))
}
targetPort = uint16(p)
} else {
targetPort = port
}
@tangx
Copy link
Author

tangx commented Nov 13, 2019

UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
固定长度的整型,包括有符号整型或无符号整型。

整型范围¶
Int8 - [-128 : 127]
Int16 - [-32768 : 32767]
Int32 - [-2147483648 : 2147483647]
Int64 - [-9223372036854775808 : 9223372036854775807]
无符号整型范围¶
UInt8 - [0 : 255]
UInt16 - [0 : 65535]
UInt32 - [0 : 4294967295]
UInt64 - [0 : 18446744073709551615]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment