#golang pop quiz: what does this code print out?
s := strings.Split("", "@") fmt.Println(len(s))
[ ] 0
[ ] 1
[ ] Undefined output
#golang pop quiz: what does this code print out?
s := strings.Split("", "@") fmt.Println(len(s))
[ ] 0
[ ] 1
[ ] Undefined output
Sounds like most of you are aware of strings.Split's behavior, and it is well documented, too:
"If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s."
So if s was an empty string, that would be the only element in the returned list.
However, it still seems a bit unintuitive to me. I keep noticing how often people expect it to return an empty slice, which leads to issues that can be tricky to debug at times.
Bobinas P4G is a social network. It runs on GNU social, version 2.0.1-beta0, available under the GNU Affero General Public License.
All Bobinas P4G content and data are available under the Creative Commons Attribution 3.0 license.