Golang中的字节(byte)和字符串(string)有时是兼容的,有时是不兼容的。

huangapple go评论91阅读模式
英文:

golang byte and string Sometimes compatible and sometimes incompatible

问题

这是我的 Golang 代码:

package test

import (
	"fmt"
	"testing"
)

func TestOne(t *testing.T) {

	bytes := make([]byte, 0)
	bytes = append(bytes, 1, 2, 3)            // 通过
	bytes = append(bytes, []byte{1, 2, 3}...) // 通过
	bytes = append(bytes, "hello"...)          // 也通过,参考:作为特例,将字符串附加到字节切片是合法的

}

func TestTwo(t *testing.T) {

	printBytes([]byte{1, 2, 3}...) // 通过
	printBytes("abcdefg"...)       // 失败

}

func printBytes(b ...byte) {
	fmt.Println(b)
}

这些是 <code>strings.Builder</code> 中的一些代码:

func (b *Builder) WriteString(s string) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s), nil
}

参数 <code>s</code> 在使用函数 <code>append</code> 时可以被视为 <code>slice</code> 类型。

但是我定义了一个类似于 <code>append</code> 的函数 <code>printBytes</code>

当我这样调用时:

printBytes("abcdefg"...)

<code>"abcdefg"</code> 似乎不被视为 <code>slice</code> 类型。

英文:

This is my golang code


package test

import (
	&quot;fmt&quot;
	&quot;testing&quot;
)

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, &quot;hello&quot;...)          // 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(&quot;abcdefg&quot;...)       // fail

}

func printBytes(b ...byte) {
	fmt.Println(b)
}


These are some code in <code>strings.Builder</code>

func (b *Builder) WriteString(s string) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s), nil
}

The param <code>s</code> can be regards as <code>slice</code> type when be used in function <code>append</code> .

But I defined a function <code>printBytes</code> like <code>append</code>,

when I invoke like this

printBytes(&quot;abcdefg&quot;...)

The <code>"abcdefg"</code> seems like not be regards as a type <code>slice</code>

答案1

得分: 7

append文档中可以看到:

> 作为一个特例,将字符串追加到字节切片是合法的,像这样:
> > slice = append([]byte("hello "), "world"...)

除了这个特例(以及copy类似情况),在Go中,string不像切片类型一样对待。

像这样的“内置”函数允许有一些特殊情况,它们不严格遵循Go的一般类型规则,因为它们的行为实际上是语言规范的一部分。请参阅追加和复制切片

英文:

From the append documentation:

> As a special case, it is legal to append a string to a byte slice, like this:
> > slice = append([]byte(&quot;hello &quot;), &quot;world&quot;...)

Other than this special case (and a similar case for copy), string is not treated like a slice type in Go.

"Built-in" functions like this are allowed to have special cases that don't strictly follow the general type rules in Go, because their behaviour is actually part of the language specification itself. See Appending to and copying slices.

huangapple
  • 本文由 发表于 2021年7月2日 12:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68219650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定