emahiro/b.log

Drastically Repeat Yourself !!!!

strings.Split は分割できない時でも長さ1の slice を返す。

https://play.golang.org/p/ss6gWvidE2u でなんで長さ 1 の slice が返ってくるんだろーって悩んでたのですが、、、

// Split slices s into all substrings separated by sep and returns a slice of
// the substrings between those separators.
//
// If s does not contain sep and sep is not empty, Split returns a
// slice of length 1 whose only element is s.
//
// If sep is empty, Split splits after each UTF-8 sequence. If both s
// and sep are empty, Split returns an empty slice.
//
// It is equivalent to SplitN with a count of -1.
func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) }

Godoc にもちゃんと 1 が返ってくるって書いてあるし、そもそも https://pkg.go.dev/strings#Split はsep に指定した文字列で分割できない時は s に指定した文字列を slice にしてそのまま返す、って言う当たり前の振る舞いを忘れてただけでした。