草庐IT

as_bytes

全部标签

go - []uint8 && []byte 之间的区别(Golang slice )

我正在运行的函数之一:image.Decode()image.Decode函数接收一个io.Reader&&,而io.Reader函数接收一个[]byte。当我传入一个[]uint8时,如果给我这个错误:panic:image:unknownformat如何将[]uint8转换为[]byte?更新错误发生在星号区域,因为image.Decode无法读取变量xxx。packagemainimport("github.com/nfnt/resize""image""image/jpeg""fmt""launchpad.net/goamz/aws""launchpad.net/goamz/s

json.Unmarshal 嵌套对象成字符串或 []byte

我正在尝试解码一些json,以便嵌套对象不会被解析,而只是被视为string或[]byte。所以我想得到以下内容:{"id":15,"foo":{"foo":123,"bar":"baz"}}解码为:typeBarstruct{IDint64`json:"id"`Foo[]byte`json:"foo"`}我收到以下错误:json:cannotunmarshalobjectintoGovalueoftype[]uint8playgrounddemo 最佳答案 我认为您正在寻找的是RawMessage输入encoding/json包。

go - 在 golang 中清除 bytes.Buffer 的正确方法是什么?

我正在尝试清除一个bytes.Buffer,但是文档中没有这个功能也许我应该更新缓冲区?正确的做法是什么?buffer=bytes.NewBufferString("")buffer.Grow(30000) 最佳答案 Packagebytesfunc(*Buffer)Resetfunc(b*Buffer)Reset()Resetresetsthebuffersoithasnocontent.b.Reset()isthesameasb.Truncate(0).func(*Buffer)Truncatefunc(b*Buffer)Tru

Golang 将 image.Image 转换为 []byte

无法将image.Image转换为[]byte。问题点用虚线包裹。image_data,err:=mybucket.Get(key)iferr!=nil{panic(err.Error())}//resetformatofdata[]bytetoimage.Imageoriginal_image,_,err:=image.Decode(bytes.NewReader(image_data))new_image:=resize.Resize(160,0,original_image,resize.Lanczos3)---------------------------//resetfor

hash - 如何从 []bytes 获取十六进制

http://play.golang.org/p/SKtaPFtnKOfuncmd(strstring)[]byte{h:=md5.New()io.WriteString(h,str)fmt.Printf("%x",h.Sum(nil))//base16,withlower-caselettersfora-freturnh.Sum(nil)}我只需要从输入字符串转换而来的哈希键字符串。我能够使用h.Sum(nil)以字节格式获取它,并且能够以%x格式打印出哈希键。但是我想从这个函数返回%x格式,以便我可以使用它将电子邮件地址转换为哈希键并使用它来访问Gravatar.com。如何在Go

go - 如何转换(类型 *bytes.Buffer)以用作 w.Write 参数中的 []byte

我正在尝试从服务器返回一些json,但使用以下代码得到此错误cannotusebuffer(type*bytes.Buffer)astype[]byteinargumenttow.Write通过谷歌搜索,我找到了thisSOanswer但无法让它工作(请参阅第二个带有错误消息的代码示例)第一个代码示例buffer:=new(bytes.Buffer)for_,jsonRawMessage:=rangesliceOfJsonRawMessages{iferr:=json.Compact(buffer,jsonRawMessage);err!=nil{fmt.Println("error"

arrays - 如何使用带有两个 []byte slice 或数组的 Go append?

我最近尝试在Go中append两个字节数组slice,但遇到了一些奇怪的错误。我的代码是:one:=make([]byte,2)two:=make([]byte,2)one[0]=0x00one[1]=0x01two[0]=0x02two[1]=0x03log.Printf("%X",append(one[:],two[:]))three:=[]byte{0,1}four:=[]byte{2,3}five:=append(three,four)错误是:cannotusefour(type[]uint8)astypeuint8inappendcannotusetwo[:](type[]u

go - 修复 "should not use basic type string as key in context.WithValue"golint

我正在使用Context和WithValue将uuid传递给处理此*http.request的后续函数。此uuid在授权header中传递给REST调用以识别人员。授权token已经过验证,需要可访问以检查调用本身是否被授权。我用过:ctx:=context.WithValue(r.Context(),string("principal_id"),*id)但是golint提示:shouldnotusebasictypestringaskeyincontext.WithValue什么是可以用来检索这个不是基本类型(如简单字符串)的键的最佳选择? 最佳答案

tcp - 如何在 Go 编程中将 []byte 转换为 int

我需要通过TCP创建一个客户端-服务器示例。在客户端,我读取了2个数字并将它们发送到服务器。我面临的问题是我无法从[]byte转换为int,因为通信只接受[]byte类型的数据.有什么方法可以将[]byte转换为int或者我可以将int发送到服务器?一些示例代码将不胜感激。谢谢。 最佳答案 您可以使用编码/二进制的ByteOrder为16、32、64位类型执行此操作Playpackagemainimport"fmt"import"encoding/binary"funcmain(){varmySlice=[]byte{244,244

go - 函数调用中的"used as value"

在条件语句中求值时调用函数的正确方法是什么?packagemainimport"fmt"funcmain(){ifsumThis(1,2)>sumThis(3,4){fmt.Println("test")}else{fmt.Println("derp")}}funcsumThis(a,bint){returna+b}这会返回错误:./test4.go:4:sumThis(1,2)usedasvalue./test4.go:4:sumThis(3,4)usedasvalue./test4.go:11:toomanyargumentstoreturn你会如何用Go写这个?