Lin*_*xea 1 arrays string byte go
这是我的 golang 代码
package test
import (
"fmt"
"testing"
)
func TestOne(t *testing.T) {
bytes := make([]byte, 0)
bytes = append(bytes, 1, 2, 3) // pass
bytes = append(bytes, []byte{1, 2, 3}...) // pass
bytes = append(bytes, "hello"...) // pass too, ok. reference: As a special case, it is legal to append a string to a byte slice
}
func TestTwo(t *testing.T) {
printBytes([]byte{1, 2, 3}...) // pass
printBytes("abcdefg"...) // fail
}
func printBytes(b ...byte) {
fmt.Println(b)
}
Run Code Online (Sandbox Code Playgroud)
这些是一些代码 strings.Builder
func (b *Builder) WriteString(s string) (int, error) {
b.copyCheck()
b.buf = append(b.buf, s...)
return len(s), nil
}
Run Code Online (Sandbox Code Playgroud)
param在 function 中使用时可以s视为slice类型append。
但是我定义的函数printBytes一样append,
当我这样调用时
printBytes("abcdefg"...)
Run Code Online (Sandbox Code Playgroud)
该"abcdefg"好像不能被作为类型slice
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |