{"id":1800,"date":"2020-03-25T14:32:36","date_gmt":"2020-03-25T14:32:36","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1800"},"modified":"2020-03-29T10:26:11","modified_gmt":"2020-03-29T10:26:11","slug":"number-characters-string-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/number-characters-string-golang\/","title":{"rendered":"Number of Characters or length of 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. All other characters are between 1 -4 bytes. That is why it is not possible to get the exact length of the string using the built-in len<\/strong> function of go. It might work if the string only contains ASCII characters. But if the string contains Non-ASCII characters then it will give the correct output.<\/p>\n\n\n\n For example, see below the program and its output. \u00a3<\/strong> in the string below is a Non-ASCII character.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n As 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 Output will be<\/p>\n\n\n\n This 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 There are two alternatives to get the correct length of a string<\/p>\n\n\n\n The below code illustrates the above two points.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\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. All other…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\npackage 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\n
a\nb\n\u00c2\n\u00a3\nLength is 5<\/code><\/pre>\n\n\n\n
sample := \"ab\u00a3c\"\nfmt.Println([]byte(sample))<\/code><\/pre>\n\n\n\n
[97 98 194 163 99]<\/code><\/pre>\n\n\n\n
a<\/td> 97<\/td><\/tr> b<\/td> 98<\/td><\/tr> \u00a3<\/td> 194, 163<\/td><\/tr> c<\/td> 99<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n package main\n\nimport \"fmt\"\n\nfunc main() {\n sample := \"ab\u00a3c\"\n \/\/Using rune\n s := []rune(sample)\n fmt.Println(len(s))\n \n \/\/Using range\n len := 0\n for range sample {\n len++\n }\n fmt.Println(len)\n}<\/code><\/pre>\n\n\n\n
4\n4<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"