How to slice a rune in Go

16 March 2015   0 comments   Go

Strangely this was hard. The solution looks easy but it took me a lot of time to get this right.

What I want to do is split a string up into a slice of parts. For example:

word := "peter"
for i := 1; i <= len(word); i++ {

The output is:


Play here.

Now, what if the word you want to split contains non-ascii characters? As a string "é" is two characters internally. So you can't split them up. See this play and press "Run".

So, the solution is to iterate over the string and Go will give you an index of each unicode character. But you'll want to skip the first one.

word := "péter"
for i, _ := range word {
    if i > 0 {

You can play with it here in this play.

Now the output is the same as what we got in the first example but with unicode characters in it.


I bet this is obvious to the gurus who've properly read the documentation but it certainly took me some time to figure it out and so I thought I'd share it.


Your email will never ever be published

Related posts

Air Mozilla on Roku 05 March 2015
Bye bye httpretty. Welcome back good old mock! 19 March 2015
Related by Keyword:
Rust > Go > Python parse millions of dates in CSV files 15 May 2018
Unzip benchmark on AWS EC2 c3.large vs c4.large 29 November 2017
Autocompeter is Dead. Long live Autocompeter! 09 January 2017
Go vs. Python 24 October 2014
String comparison function in Python (alpha) 22 December 2007
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
Ever wondered how much $87 Billion is? 04 November 2003
Guake, not Yakuake or Yeahconsole 23 January 2010