bytes.Split的分隔符为[]byte(“…”)

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

bytes.Split separator as []byte("...")

问题

bytes_test.go中,我看到:

 a := Split([]byte(tt.s), []byte(tt.sep), tt.n)

其中tt.s和tt.sep是字符串。但是当我尝试这样做时:

 a := bytes.Split([]byte("test"), []byte("e"), 0)

我得到:

 无法将"test"(类型为ideal string)转换为类型[]uint8
 无法将"e"(类型为ideal string)转换为类型[]uint8
英文:

In bytes_test.go I see:

 a := Split([]byte(tt.s), []byte(tt.sep), tt.n)

where tt.s and tt.sep are strings. But when I try to do

 a := bytes.Split([]byte("test"), []byte("e"), 0)

I get:

 cannot convert "test" (type ideal string) to type []uint8 in conversion
 cannot convert "e" (type ideal string) to type []uint8 in conversion

答案1

得分: 5

以下是使用最新版本的有效代码--release.2010-03-04--其中包括了以下更改:"有一个语言更改:能够将字符串转换为[]byte或[]int。这使得strings.Bytes和strings.Runes函数过时。"

package main

import ("bytes"; "fmt")

func main() {
    a := bytes.Split([]byte("test"), []byte("e"), 0)
    fmt.Println(a)
}

更新到当前版本的Go:安装Go:跟上版本

英文:

The following is valid code using the latest release -- release.2010-03-04 -- which includes, amongst other things, this change: "There is one language change: the ability to convert a string to []byte or []int. This deprecates the strings.Bytes and strings.Runes functions."

package main

import ("bytes"; "fmt")

func main() {
	a := bytes.Split([]byte("test"), []byte("e"), 0)
	fmt.Println(a)
}

Update to a current release of Go: Installing Go : Keeping up with releases.

huangapple
  • 本文由 发表于 2010年3月6日 07:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/2390619.html
匿名

发表评论

匿名网友

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

确定