{"id":1750,"date":"2020-03-16T21:24:17","date_gmt":"2020-03-16T21:24:17","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1750"},"modified":"2020-03-16T21:24:26","modified_gmt":"2020-03-16T21:24:26","slug":"reverse-a-string-in-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/reverse-a-string-in-golang\/","title":{"rendered":"Reverse 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.\u00a0 In GO, rune data type represents a Unicode point.\u00a0 Once a string is converted to an array of rune then it is possible to index a character in that array of rune<\/strong>. You can learn more about rune here –\u00a0https:\/\/golangbyexamples.com\/understanding-rune-in-golang<\/a><\/p>\n\n\n\n For this reason in the below program for reverse a string,\u00a0 we are first converting a string into a rune array so that we can index the rune array to get the individual characters. Once we have the individual characters, we can keep appending it to a new string starting from the end to start.<\/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.\u00a0All 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-1750","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n sample := \"ab\u00a3d\"\n r := []rune(sample)\n var res []rune\n for i := len(r) - 1; i >= 0; i-- {\n res = append(res, r[i])\n }\n fmt.Printf(\"Result: %s\\n\", string(res))\n}<\/code><\/pre>\n\n\n\n
Result: d\u00a3ba<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"