Skip to content

strings: Split returns an array of length one for an empty string with non-empty separator #54455

@roskee

Description

@roskee

What version of Go are you using (go version)?

1.19

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go playground

What did you do?

I tried to split an empty string with a non-empty separator using strings.Split method.

values := strings.Split("", ",")
fmt.Println(len(values))

The result was an array of length one with a value of an empty string at index 0. But if the separator is empty the result becomes an array of length 0. But the separator shouldn't have made a difference on the result since the string I am trying to split is empty in the first place.
here is the link to my program.
playground link

What did you expect to see?

The output should have been 0 since the strings I wanted to split is empty in the first place.

What did you see instead?

It resulted in an array of length 1. which has an empty string at index 0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions