Proposal Details
func main() {
b1 := [6]byte{'a', 'b', 'c', 'd', 'e', 'f'}
s1 := string(b1[:])
b2 := [10]byte{'a', 'b', 'c', 'd', 'e', 'f'}
s2 := string(b2[:])
s3 := "abcdef"
fmt.Println(s1 == s2)
fmt.Println(s2 == s3)
fmt.Println(s1 == s3)
fmt.Println(s1, len(s1))
fmt.Println(s2, len(s2))
fmt.Println(s3, len(s3))
b4 := [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 0, 0, 0, 'k'}
s4 := string(b4[:])
fmt.Println(s4, len(s4))
}
cmd out
false
false
true
abcdef 6
abcdef 10
abcdef 6
abcdefk 10
When converting slice or array to string, the 0 are not cut. This is not very friendly for use array
Comment From: seankhliao
I don't think this is feasible in any way, there's no way to determine if 0 bytes are intended data.
Comment From: zigo101
Not only 0, there are more byte values which don't represent visible characters.
Comment From: ameise84
Not only 0, there are more byte values which don't represent visible characters.
I expect bytes to string, which of course means that all the characters inside should be visible or meaningful, such as \n
Comment From: ameise84
I don't think this is feasible in any way, there's no way to determine if 0 bytes are intended data.
'\0' is used to truncate strings in ASCII. who want see `\0'? It is invisible
Comment From: ameise84
I can understand that strings in golang do not retain '\0' as an end processing, but I cannot understand that bytes do not process '\0' when converting to strings
Comment From: randall77
Go strings are just byte sequences. There's no "process" when converting to strings, the string contains exactly the same bytes as the []byte source does.
The only time strings are interpreted as having characters (in the language) is in for range
statements.
You would need to use unicode/utf8
or for range
to make runes from the string, then the unicode
package to decide if characters are visible, etc.
Comment From: ameise84
Go strings are just byte sequences. There's no "process" when converting to strings, the string contains exactly the same bytes as the []byte source does. The only time strings are interpreted as having characters (in the language) is in
for range
statements. You would need to useunicode/utf8
orfor range
to make runes from the string, then theunicode
package to decide if characters are visible, etc.
if use string([]byte), then means the characters in []byte are interpreted according to ASCII, not other encoding formats such as UTF-8 or UTF-16. As you said, the string ([]byte) method should not be provided. This does not conform to understanding of strings. Why does the character '\0' exist in a string, it is invisible and has no effect
Comment From: ianlancetaylor
We find that Go strings work well. In any case we aren't going to change anything now.
https://go.dev/ref/spec#String_types https://go.dev/ref/spec#Conversions section "Conversions to and from a string type"
Comment From: ameise84
We find that Go strings work well. In any case we aren't going to change anything now.
https://go.dev/ref/spec#String_types https://go.dev/ref/spec#Conversions section "Conversions to and from a string type"
I know go strings work very well, and I also like string without '\0'. But this should not violate ASCII's interpretation of the '\0' character. Only a binary search method is needed to perfectly solve the problem.