The-Golang-Standard-Library-by-Example icon indicating copy to clipboard operation
The-Golang-Standard-Library-by-Example copied to clipboard

2.1.6 字符串 JOIN 函数, 实现方法已经修改

Open tan00 opened this issue 6 years ago • 0 comments

关于join函数的说明, 可能需要修改

标准库的实现没有用 bytes 包,当然也不会简单的通过 + 号连接字符串

目前查看标准库实现方式是使用了 += 字符串连接操作的.

func Join(a []string, sep string) string {
	switch len(a) {
	case 0:
		return ""
	case 1:
		return a[0]
	case 2:
		// Special case for common small values.
		// Remove if golang.org/issue/6714 is fixed
		return a[0] + sep + a[1]
	case 3:
		// Special case for common small values.
		// Remove if golang.org/issue/6714 is fixed
		return a[0] + sep + a[1] + sep + a[2]
	}
        ...
$ go version
go version go1.10 windows/amd64

该版本也没有引入 bytes 包. 但直接使用了 += 连接符

tan00 avatar Oct 22 '18 08:10 tan00