代码之家  ›  专栏  ›  技术社区  ›  idavydov

字节。将分隔符拆分为[]字节(“…”)

  •  2
  • idavydov  · 技术社区  · 16 年前

    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)
    

    我得到:

     cannot convert "test" (type ideal string) to type []uint8 in conversion
     cannot convert "e" (type ideal string) to type []uint8 in conversion
    
    1 回复  |  直到 16 年前
        1
  •  4
  •   peterSO    16 年前

    以下是使用最新版本的有效代码-- release.2010-03-04 --其中包括,除其他外,这一变化:“有一种语言变化:将字符串转换为[]字节或[]int的能力。这会贬低字符串、字节和字符串。运行函数。”

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

    更新到Go的当前版本: Installing Go : Keeping up with releases .