How to split a string on whitespace in Go?
Hello everyone! I just started learning Go and I have a problem. I need to split a string into words, ignoring all whitespace characters (spaces, tabs, newlines).
I tried using `strings.Split()`, but it only works with a single delimiter. For example, if I write:
```go
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world\tfoo\nbar"
parts := strings.Split(s, " ")
fmt.Println(parts)
// ["hello", "", "", "world\tfoo\nbar"]
}
```
It becomes a mess — empty strings, tabs are not handled.
I've seen that in other languages (Python, Java) there are built-in `split()` methods without arguments that automatically handle any whitespace. Is there something similar in Go? Or do I need to write my own function? I heard it can be done using `re
No answers yet.
Log in to leave an answer