Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings.Split a empty string, and len([]string) equal 1 #39806

Closed
orange-jacky opened this issue Jun 24, 2020 · 1 comment
Closed

strings.Split a empty string, and len([]string) equal 1 #39806

orange-jacky opened this issue Jun 24, 2020 · 1 comment

Comments

@orange-jacky
Copy link

orange-jacky commented Jun 24, 2020

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

$ go version
go version go1.14 darwin/amd64

Does this issue reproduce with the latest release?

vi test.go

package main

import (
	"fmt"
	"strings"
)

func main() {
	src := ""
	slice := strings.Split(src, ",")
	fmt.Printf("src=%q slice=%+v len(slice)=%d\n", src, slice, len(slice))
}
% go run test.go
src="" slice=[] len(slice)=1

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/fredlee/Documents/develop/go/workspace/bin"
GOCACHE="/Users/fredlee/Library/Caches/go-build"
GOENV="/Users/fredlee/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/fredlee/Documents/develop/go/workspace"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/fredlee/Documents/xxx/xxx/xx/xxx/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/34/z2z_vp1573g5014m8k7wys100000gn/T/go-build085642700=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.14 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.14
uname -v: Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.15.1
BuildVersion:	19B88
lldb --version: lldb-1100.0.30.6
Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)

What did you do?

What did you expect to see?

src="" slice=[] len(slice)=0

What did you see instead?

src="" slice=[] len(slice)=1

@davecheney
Copy link
Contributor

Thank you for raising this issue. Based on the documentation for strings.Split I believe this is working as intended

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

Based on this I am closing this issue. Please comment if you disagree but keep in mind that the operation of this function is covered by the Go 1 contract.

@golang golang locked and limited conversation to collaborators Jun 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants