Why are empty arrays returned from strings .Split has a length of 1 in golang?

I am just starting to learn golangs, and I came across something rather strange. When you get an empty array from a call to string.Split, it has a length of one.

Example

package main

import (
    "fmt"
    "strings"
)

func main() {
    test := strings.Split("", ",")

    fmt.Println(test)
    fmt.Println(len(test))
}

It is output:

[]
1

Why is this? If this is the expected behavior, what is the correct way to check if the array is empty?

thank

+4
source share
1 answer

As @u_mulder said in the comments, the array is not empty, as it contains an empty string.

+2
source

All Articles