在Go语言中,我如何将[]myByte转换为[]byte?

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

In Go, how do I convert []myByte to []byte?

问题

我有一个<code>type myByte byte</code>,我使用它是因为我想在逻辑上区分不同类型的字节。

我可以使用<code>byte(myByte(1))</code>轻松转换,

但是我找不到一种方法来转换或转换一个切片:<code>[]byte([]myByte{1})</code>失败。

这种事情可能吗?内存中的位是相同的(对吗?),所以应该有一些方法,而不是逐字节复制到一个新对象中...

例如,这些都不起作用:http://play.golang.org/p/WPhD3KufR8

package main

type myByte byte

func main() {
    a := []myByte{1}

    fmt.Print(byte(myByte(1))) // 正常工作

    fmt.Print([]byte([]myByte{1})) // 失败:无法将[]myByte字面量(类型[]myByte)转换为类型[]byte

    // 无法将a(类型[]myByte)作为类型[]byte传递给函数参数
    // fmt.Print(bytes.Equal(a, b))
    
    // 无法将a(类型[]myByte)转换为类型[]byte
    // []byte(a)
    
    // panic: 接口转换:接口是[]main.myByte,而不是[]uint8
    // abyte := (interface{}(a)).([]byte)
}
英文:

I have a <code>type myByte byte</code> that I use because I want to logically differentiate different kinds of bytes.

I can convert easily with <code>byte(myByte(1))</code>,

but I can't find away to cast or convert an slice: <code>[]byte([]myByte{1})</code> fails.

Is such a thing possible? The bits are the same in memory (right?) so there should be some way, short of copying byte by byte into a new object..

For example, none of this works: http://play.golang.org/p/WPhD3KufR8

package main

type myByte byte

func main() {
a := []myByte{1}

fmt.Print(byte(myByte(1))) // Works OK

fmt.Print([]byte([]myByte{1})) // Fails: cannot convert []myByte literal (type []myByte) to type []byte

// cannot use a (type []myByte) as type []byte in function argument
// fmt.Print(bytes.Equal(a, b))

// cannot convert a (type []myByte) to type []byte
// []byte(a)

// panic: interface conversion: interface is []main.myByte, not []uint8
// abyte := (interface{}(a)).([]byte)
}

答案1

得分: 4

你不能将你自己的myByte的切片转换为byte的切片。

但是你可以拥有自己的字节切片类型,可以将其转换为字节切片:

package main

import "fmt"

type myBytes []byte

func main() {
    var bs []byte
    bs = []byte(myBytes{1, 2, 3})
    fmt.Println(bs)
}

根据你的问题,这可能是一个不错的解决方案。
(你无法区分来自myBytes的字节和来自byte的字节,但是你的切片是类型安全的。)

英文:

You cannot convert slices of your own myByte to a slice of byte.

But you can have your own byte-slice type which can be cast to
a byte slice:

package main

import &quot;fmt&quot;

type myBytes []byte

func main() {
     var bs []byte
     bs = []byte(myBytes{1, 2, 3})
     fmt.Println(bs)
}

Depending on your problem this might be a nice solution.
(You cannot distinguish a byte from myBytes from a byte,
but your slice is typesafe.)

答案2

得分: 1

显然,没有其他方法,解决方案只是循环遍历整个切片,将每个元素转换并复制到一个新的切片,或者将类型转换“推迟”到每个元素操作中。

英文:

Apparently, there is no way, and the solution is just to loop over the whole slice converting each element and copying to a new slice or "push down" the type conversion to the per-element operations.

https://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go

huangapple
  • 本文由 发表于 2013年7月25日 11:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/17848725.html
匿名

发表评论

匿名网友

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

确定