<\/span><\/h2>\n\n\n\nutf-8 saves every Unicode Point either using 1, 2, 3 or 4 bytes. ASCII points are stored using 1 byte. That is why rune is an alias for int32 because a Unicode Point can be of max 4 bytes in Go as source code in GO is encoded using utf-8, hence every string is also encoded in utf-8<\/p>\n\n\n\n
Every rune is intended to refer to one Unicode Point.\u00a0For eg if you print a string after typecasting it to a rune array then it will print the Unicode Point for each of character. For for below string “0b\u00a3”<\/strong> output will be – [U+0030 U+0062 U+00A3]<\/strong><\/p>\n\n\n\nfmt.Printf(\"%U\\n\", []rune(\"0b\u00a3\"))<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>When to Use<\/strong><\/span><\/h1>\n\n\n\nYou should use a rune when you intend to save Unicode Code Point in the rune value. A rune array should be used when all values in the array are meant to be a Unicode Code Point.<\/li><\/ul>\n\n\n\nRune is also used to represent a character.<\/li><\/ul>\n\n\n\nDeclare Rune<\/strong><\/p>\n\n\n\nA rune is declared using a character between single quotes like below declaring a variable named ‘rPound’<\/strong><\/p>\n\n\n\nrPound := '\u00a3'<\/code><\/pre>\n\n\n\nAfter declaring Rune you can perform below things as well<\/p>\n\n\n\n
Print Type – <\/strong>Output will be int32<\/strong><\/li><\/ul>\n\n\n\nfmt.Printf(\"Type: %s\\n\", reflect.TypeOf(rPound))<\/code><\/pre>\n\n\n\nPrint Unicode Code Point – <\/strong>Output will be U+00A3<\/strong><\/li><\/ul>\n\n\n\nfmt.Printf(\"Unicode CodePoint: %U\\n\", rPound)<\/code><\/pre>\n\n\n\nPrint Character – <\/strong>Output will be \u00a3<\/strong><\/li><\/ul>\n\n\n\nfmt.Printf(\"Character: %c\\n\", r)<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Code:<\/strong><\/span><\/h1>\n\n\n\nBelow is the code illustrating each point we discussed<\/p>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"reflect\"\n \"unsafe\"\n)\n\nfunc main() {\n r := 'a'\n \n \/\/Print Size\n fmt.Printf(\"Size: %d\\n\", unsafe.Sizeof(r))\n \n \/\/Print Type\n fmt.Printf(\"Type: %s\\n\", reflect.TypeOf(r))\n \n \/\/Print Code Point\n fmt.Printf(\"Unicode CodePoint: %U\\n\", r)\n \n \/\/Print Character\n fmt.Printf(\"Character: %c\\n\", r)\n s := \"0b\u00a3\"\n \n \/\/This will print the Unicode Points\n fmt.Printf(\"%U\\n\", []rune(s))\n \n \/\/This will the decimal value of Unicode Code Point\n fmt.Println([]rune(s))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nSize: 4\nType: int32\nUnicode CodePoint: U+0061\nCharacter: a\n[U+0030 U+0062 U+00A3]\n[48 98 163]<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Rune array to string and vice versa<\/strong><\/span><\/h1>\n\n\n\n<\/span>Rune array to string<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n runeArray := []rune{'a', 'b', '\u00a3'}\n s := string(runeArray)\n fmt.Println(s)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nab\u00a3<\/code><\/pre>\n\n\n\n<\/span>String to Rune Array<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n s := \"ab\u00a3\"\n r := []rune(s)\n fmt.Printf(\"%U\\n\", r)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n[U+0061 U+0062 U+00A3]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewWhat is UnicodeUTF-8When to UseCode:Rune array to string and vice versaRune array to stringString to Rune Array Overview rune in Go is\u00a0an alias for int32 meaning it is an…<\/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":[3,147],"class_list":["post-1200","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-rune"],"yoast_head":"\n
Understanding Rune in Golang - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n