{"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\n

Output:<\/strong><\/p>\n\n\n\n

a\nb\n\u00c2\n\u00a3\nLength is 5<\/code><\/pre>\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

sample := \"ab\u00a3c\"\nfmt.Println([]byte(sample))<\/code><\/pre>\n\n\n\n

The output will be<\/p>\n\n\n\n

[97 98 194 163 99]<\/code><\/pre>\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

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

Then how we can index into a string. This is where rune<\/strong> data type comes into picture In GO, rune<\/strong> data type represents a Unicode point.\u00a0You can learn more about rune here - https:\/\/golangbyexamples.com\/understanding-rune-in-golang<\/a><\/p>\n\n\n\n

Once a string is converted to an array of rune<\/strong> then it is possible to index a character in that array of rune. See below code<\/p>\n\n\n\n

package main\n\nimport \"fmt\"\n\nfunc main() {\n    sample := \"ab\u00a3c\"\n    sampleRune := []rune(sample)\n\n    fmt.Printf(\"%c\\n\", sampleRune[0])\n    fmt.Printf(\"%c\\n\", sampleRune[1])\n    fmt.Printf(\"%c\\n\", sampleRune[2])\n    fmt.Printf(\"%c\\n\", sampleRune[3])\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

a\nb\n\u00a3\nc<\/code><\/pre>\n\n\n\n

Also to mention you can use range operator to iterate over all Unicode characters in the string, but to index character in a string, you can convert it to an array of rune<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"

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":[4,215],"class_list":["post-1727","post","type-post","status-publish","format-standard","hentry","category-tech","tag-golang","tag-index"],"yoast_head":"\nIndex character in a string in Go (Golang) - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/go-index-character-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Index character in a string in Go (Golang) - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"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...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/go-index-character-string\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-16T18:33:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-16T18:35:25+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/go-index-character-string\/\",\"url\":\"https:\/\/golangbyexamples.com\/go-index-character-string\/\",\"name\":\"Index character in a string in Go (Golang) - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2020-03-16T18:33:01+00:00\",\"dateModified\":\"2020-03-16T18:35:25+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/go-index-character-string\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/go-index-character-string\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/go-index-character-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Index character in a string in Go (Golang)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Index character in a string in Go (Golang) - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/go-index-character-string\/","og_locale":"en_US","og_type":"article","og_title":"Index character in a string in Go (Golang) - Welcome To Golang By Example","og_description":"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...","og_url":"https:\/\/golangbyexamples.com\/go-index-character-string\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2020-03-16T18:33:01+00:00","article_modified_time":"2020-03-16T18:35:25+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/go-index-character-string\/","url":"https:\/\/golangbyexamples.com\/go-index-character-string\/","name":"Index character in a string in Go (Golang) - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2020-03-16T18:33:01+00:00","dateModified":"2020-03-16T18:35:25+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/go-index-character-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/go-index-character-string\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/go-index-character-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Index character in a string in Go (Golang)"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-rR","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=1727"}],"version-history":[{"count":5,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1727\/revisions"}],"predecessor-version":[{"id":1733,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1727\/revisions\/1733"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=1727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=1727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=1727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}