In Python if I do...:
parts = "".split(",")
print parts, len(parts)
The output is:
[], 0
If I do the equivalent in Go...:
parts = strings.Split("", ",")
fmt.Println(parts, len(parts))
the output is:
[], 1
How can it have a length of 1 if there's nothing in it?
The result of strings.Split
is a slice with one element - the empty string.
fmt.Println
is just not displaying it. Try this example (notice the change to the last print).
package main
import "fmt"
import "strings"
func main() {
groups := strings.Split("one,two", ",")
fmt.Println(groups, len(groups))
groups = strings.Split("one", ",")
fmt.Println(groups, len(groups))
groups = strings.Split("", ",")
fmt.Printf("%q, %d
", groups, len(groups))
}
This makes sense. If you wanted to split the string "HelloWorld"
using a ,
character as the delimiter, you'd expect the result to be "HelloWorld"
- the same as your input.