這是我的戈朗代碼package testimport ( "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)}這些是strings.Builderfunc (b *Builder) WriteString(s string) (int, error) { b.copyCheck() b.buf = append(b.buf, s...) return len(s), nil}在函數 中使用該參數時,可以將其視為類型。ssliceappend但是我定義了一個函數,如,printBytesappend當我像這樣調用時printBytes("abcdefg"...)似乎不被視為一種類型"abcdefg"slice
戈朗字節和字符串 有時兼容,有時不兼容
慕婉清6462132
2022-09-12 16:25:17