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++ {
    fmt.Println(word[0:i])
}

The output is:

p
pe
pet
pete
peter

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 {
        fmt.Println(word[0:i])
    }
}
fmt.Println(word)

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.

p
pé
pét
péte
péter

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.

Comments

malko

You should prefer to convert your string to a slice of rune to correctly iterate on a string character by character:

  word := []rune("péter")
  for i := 1; i <= len(word); i++ {
      fmt.Println(word[0:i])
  }

This will handle utf8 characters properly. Iterating on a string your are iterating on a slice of bytes which is different from characters.

Your email will never ever be published.

Previous:
Air Mozilla on Roku March 5, 2015 Mozilla
Next:
Bye bye httpretty. Welcome back good old mock! March 19, 2015 Python
Related by category:
Converting Celsius to Fahrenheit round-up July 22, 2024 Go
Converting Celsius to Fahrenheit with Go July 17, 2024 Go
Unzip benchmark on AWS EC2 c3.large vs c4.large November 29, 2017 Go
Go vs. Python October 24, 2014 Go
Related by keyword:
Simple object lookup in TypeScript June 14, 2024 JavaScript
Fastest Python function to slugify a string September 12, 2019 Python
Umlauts (non-ascii characters) with git on macOS March 22, 2021 Python, macOS
Rust > Go > Python ...to parse millions of dates in CSV files May 15, 2018 Python