{"id":1727,"date":"2020-03-16T18:33:01","date_gmt":"2020-03-16T18:33:01","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1727"},"modified":"2020-03-16T18:35:25","modified_gmt":"2020-03-16T18:35:25","slug":"go-index-character-string","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/go-index-character-string\/","title":{"rendered":"Index character in a string in Go (Golang)"},"content":{"rendered":"\n
In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters.\u00a0All other characters are between 1 -4 bytes. Due to this, it is not possible to index a character in a string.<\/p>\n\n\n\n
\u00a0For example, see below the program and its output.<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n sample := \"ab\u00a3c\"\n for i := 0; i < 4; i++ {\n fmt.Printf(\"%c\\n\", sample[i])\n }\n fmt.Printf(\"Length is %d\\n\", len(sample))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\na\nb\n\u00c2\n\u00a3\nLength is 5<\/code><\/pre>\n\n\n\nAs you might have noticed, it prints different characters than expected and length is also 5 instead of 4. Why is that? To answer please remember we said that a string is essentially a slice of bytes. Let's print that slice of bytes using<\/p>\n\n\n\n
sample := \"ab\u00a3c\"\nfmt.Println([]byte(sample))<\/code><\/pre>\n\n\n\nThe output will be<\/p>\n\n\n\n
[97 98 194 163 99]<\/code><\/pre>\n\n\n\nThis is the mapping of each of character to its byte sequence. As you can notice a<\/strong>, b<\/strong>, c<\/strong> take each 1 byte but \u00a3<\/strong> takes two bytes. That is why the length of the string is 5 and not 4<\/p>\n\n\n\n