如何在Go中将空字符串拆分为字符串

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))
}

Playground link

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.