Go中的字符串和[]字节有什么区别? [英] What is the difference between the string and []byte in Go?

查看:152
本文介绍了Go中的字符串和[]字节有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  s:=某个字符串
b:= [] byte(s)//转换字符串 - > [] byte
s2:= string(b)// convert [] byte - >字符串

字符串 []字节在Go?



何时使用he或她?



为什么?

  bb:= [] byte {'h','e ','l','l','o',127} 
ss:= string(bb)
fmt.Println(ss)
pre>


hello


只是hello,缺少 127 ,有时我觉得很奇怪。

解决方案

string [] byte 是不同的类型,但它们可以互相转换


3。将一段字节转换为一个字符串类型会产生一个字符串,其连续字节是该切片的元素。



4。将字符串类型的值转换为字节片段类型会生成一个片段,其连续元素为字符串的字节。


Blog :数组,切片(和字符串):'append'的机制:


字符串实际上非常简单:它们只是字节的只读片段,并且有一点额外语言的语法支持。


另请阅读: Go <中的字符串,字节,符文和字符

何时使用另一个?



取决于你需要什么。字符串是不可变的,所以它们可以被共享,并且你可以保证它们不会被修改。

字节片可以被修改(意味着支持数组的内容)。

另外,如果您需要频繁地将字符串转换为 [] byte (例如,因为您需要将它写入 io.Writer () ),您应该首先考虑将其存储为 [] byte



另请注意,您可以使用字符串 常量,但没有切片常量。这可能是一个小的优化。还需要注意的是:
$ b


如果<$ $ len(s) c $ c $ s是一个字符串常量。

另外,如果你正在使用已经写好的代码标准库,第三方软件包或您自己的软件包),在大多数情况下,它会给出您必须传递或返回的参数和值。例如。如果您从 io.Reader ,你需要有一个 []字节,你必须传递它来接收读取字节,你不能使用字符串<
$ b




这个例子:




pre> bb:= [] byte {'h','e','l','l','o',127}

这里发生的是您使用了复合文字(片面文字)来创建和初始化一个类型为 [] byte 的新片段(使用短变量声明)。您指定常量以列出切片的初始元素。您还使用了一个字节值 127 ,这取决于平台/控制台 - 可能有也可能没有可视化表示。


s := "some string"
b := []byte(s) // convert string -> []byte
s2 := string(b) // convert []byte -> string

what is the difference between the string and []byte in Go?

When to use "he" or "she"?

Why?

bb := []byte{'h','e','l','l','o',127}
ss := string(bb)
fmt.Println(ss)

hello

The output is just "hello", and lack of 127, sometimes I feel that it's weird.

解决方案

string and []byte are different types, but they can be converted to one another:

3 . Converting a slice of bytes to a string type yields a string whose successive bytes are the elements of the slice.

4 . Converting a value of a string type to a slice of bytes type yields a slice whose successive elements are the bytes of the string.

Blog: Arrays, slices (and strings): The mechanics of 'append':

Strings are actually very simple: they are just read-only slices of bytes with a bit of extra syntactic support from the language.

Also read: Strings, bytes, runes and characters in Go

When to use one over the other?

Depends on what you need. Strings are immutable, so they can be shared and you have guarantee they won't get modified.

Byte slices can be modified (meaning the content of the backing array).

Also if you need to frequently convert a string to a []byte (e.g. because you need to write it into an io.Writer()), you should consider storing it as a []byte in the first place.

Also note that you can have string constants but there are no slice constants. This may be a small optimization. Also note that:

The expression len(s) is constant if s is a string constant.

Also if you are using code already written (either standard library, 3rd party packages or your own), in most of the cases it is given what parameters and values you have to pass or are returned. E.g. if you read data from an io.Reader, you need to have a []byte which you have to pass to receive the read bytes, you can't use a string for that.


This example:

bb := []byte{'h','e','l','l','o',127}

What happens here is that you used a composite literal (slice literal) to create and initialize a new slice of type []byte (using Short variable declaration). You specified constants to list the initial elements of the slice. You also used a byte value 127 which - depending on the platform / console - may or may not have a visual representation.

这篇关于Go中的字符串和[]字节有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆