英文:
Difference between []uint8 && []byte (Golang Slices)
问题
我正在运行的一个函数是:image.Decode()
image.Decode函数接受一个io.Reader类型的参数,而io.Reader函数接受一个[]byte类型的参数。
当我传入一个[]uint8类型的参数时,会出现以下错误:
panic: image: unknown format
如何将[]uint8转换为[]byte?
更新
错误发生在星号标记的位置,因为image.Decode无法读取变量xxx。
package main
import (
"github.com/nfnt/resize"
"image"
"image/jpeg"
"fmt"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"bytes"
"encoding/json"
"io/ioutil"
"os"
"reflect"
)
type Data struct {
Key string
}
func main() {
useast := aws.USEast
connection := s3.New(auth, useast)
mybucket := connection.Bucket("bucketName")
image_data, err := mybucket.Get("1637563605030")
if err != nil {
panic(err.Error())
} else {
fmt.Println("success")
}
xxx := []byte(image_data)
******* 错误发生在这里 **************
original_image, _, err := image.Decode(bytes.NewReader(xxx))
******* 错误发生在这里结束 **************
if err != nil {
fmt.Println("Shit")
panic(err.Error())
} else {
fmt.Println("Another success")
}
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
if new_image != nil {
fmt.Println("YAY")
}
}
英文:
One of the functions I am running: image.Decode()
The image.Decode function takes in an io.Reader && and the io.Reader function takes in a []byte.
When I pass in a []uint8, if gives me this error:
panic: image: unknown format
How do I convert the []uint8 to []byte?
UPDATE
The error is happening at the starred area because image.Decode can't read the variable xxx.
package main
import (
"github.com/nfnt/resize"
"image"
"image/jpeg"
"fmt"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"bytes"
"encoding/json"
"io/ioutil"
"os"
"reflect"
)
type Data struct {
Key string
}
func main() {
useast := aws.USEast
connection := s3.New(auth, useast)
mybucket := connection.Bucket("bucketName")
image_data, err := mybucket.Get("1637563605030")
if err != nil {
panic(err.Error())
} else {
fmt.Println("success")
}
xxx := []byte(image_data)
******* THIS IS WHERE THE ERROR OCCURS **************
original_image, _, err := image.Decode(bytes.NewReader(xxx))
******* THIS IS WHERE THE ERROR OCCURS END **************
if err != nil {
fmt.Println("Shit")
panic(err.Error())
} else {
fmt.Println("Another success")
}
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
if new_image != nil {
fmt.Println("YAY")
}
}
答案1
得分: 61
《Go编程语言规范》
数值类型
uint8 所有无符号8位整数的集合(0到255)
byte uint8的别名
package main
import "fmt"
func ByteSlice(b []byte) []byte { return b }
func main() {
b := []byte{0, 1}
u8 := []uint8{2, 3}
fmt.Printf("%T %T\n", b, u8)
fmt.Println(ByteSlice(b))
fmt.Println(ByteSlice(u8))
}
输出:
[]uint8 []uint8
[0 1]
[2 3]
你误诊了你的问题。
英文:
> The Go Programming Language Specification
>
> Numeric types
>
> uint8 the set of all unsigned 8-bit integers (0 to 255)
>
> byte alias for uint8
package main
import "fmt"
func ByteSlice(b []byte) []byte { return b }
func main() {
b := []byte{0, 1}
u8 := []uint8{2, 3}
fmt.Printf("%T %T\n", b, u8)
fmt.Println(ByteSlice(b))
fmt.Println(ByteSlice(u8))
}
Output:
[]uint8 []uint8
[0 1]
[2 3]
You have misdiagnosed your problem.
答案2
得分: 7
如其他答案所解释的那样,将[]uint8
传递给需要[]byte
的情况是没有问题的。如果这是你的问题,你会得到一个编译时错误。但你没有得到编译时错误,而是得到了一个运行时错误,这是由图像库在读取切片中的数据时抛出的panic错误。
实际上,图像库只是部分引起了你的问题。请参考http://golang.org/src/pkg/image/format.go。它返回一个错误消息,因为它无法识别切片中数据的图像格式。你的代码调用了image.Decode()
,当image.Decode()
返回错误消息时,它会调用panic
。
英文:
As the other answers have explained, there's no problem passing a []uint8
where a []byte
is required. If this was your problem, you'd be getting a compile time error. You aren't. A panic is a runtime error, and it's being thrown by the image library when it reads the data in the slice.
In fact, the image library is only partially your problem. See http://golang.org/src/pkg/image/format.go. It's returning an error message because it doesn't recognize the image format of the data in the slice. Your code, which calls image.Decode()
is calling panic
when image.Decode()
returns the error message.
答案3
得分: -1
如果你有一个变量imageData
,类型是[]uint8
,你可以传递[]byte(imageData)
。
参考:http://golang.org/ref/spec#Conversions
英文:
If you have a variable imageData
that is []uint8
you may pass []byte(imageData)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论