be, at most, uint32 max so 4294967295 characters.
And each character is a byte so 34359738360 bits is a bit more than 4 gigabytes, this is for 32-bit systems. 64 is larger but aiming for 32-bit will probably be the safest for compatibility.
Can someone check my calculations?
You can use len() function to get the length of a string, and len() returns a number with int type. Also from the runtime representation of a string, the Len field is of type int. And the int type is usually 32 bits on 32-bit systems and 64 bits on 64-bit systems.
It’s int instead of uint? That seems inefficient. Can strings be negative length?
its useless but not a problem int of 64bits can represent until 9223372036854775807 -> over 8000 petabytes..
Character not always 1 bite
I know, a character is a rune. I was talking about an ASCII char actually
Why do you think int is less efficient than uint? uint has a few inconspicuous bugs
Such as? All I know is uint should be used for when no negative numbers are required. This is a clear example.
You can run this piece of code package main import "fmt" func main() { var a uint8 s := "This is string" a = uint8(len(s)) - 1 for ; a >= 0; a-- { fmt.Printf("%c\n", s[a]) } }
Indexing strings by position is bad
This is just an example. s can be any type which can be iterated, such as a slice or an array.
You said “bug” this is not a bug. It’s literally an unsigned int, which has double capacity for positive numbers vs the same bit length int
Обсуждают сегодня