本文介绍了将任意Golang接口转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图编写一个可以接受所有数据类型的散列。一旦进入函数,我将数据作为字节数组处理。我无法确定如何将任意 interface {} 转换为字节数组。使用二进制包,但它似乎取决于传入的数据类型。 Write() fn (docs)知道参数的字节顺序。 所有数据类型大小都是一些倍数一个字节(即使是布尔),所以这应该是理论上的简单。 下面的代码, package bloom import(encoding / gobbytes) //改编自http://bretmulvey.com/hash/7.html func ComputeHash(key interface {})(uint,error){ var buf bytes.Buffer enc:= gob.NewEncoder(& buf) err:= enc.Encode(key) if err!= nil { return 0,err } data:= buf.Bytes() var a,b,c uint a, b = 0x9e3779b9,0x9e3779b9 c = 0; i:= 0; for i = 0;我< LEN(数据)-12; {a + = uint(data [i + 1] | data [i + 2]i + = 4 b + = uint(data [i + 1] | data [i + 2] a,b,c = mix(a,b,c); } c + = uint(len(data)) if i< len(data){ a + = uint(data [i]) i ++ } if i< len(data){ a + = uint(data [i] i ++ } if i a + = uint(data [i] i ++ } if i a + = uint(data [i] i ++ } if i< len(data){ b + = uint(data [i]) i ++ } if i< len(data){ b + = uint(data [i] i ++ } if i< len(data){ b + = uint(data [i] i ++ } if i< len(data){ b + = uint(data [i] i ++ } if i< len(data){ c + = uint(data [i] i ++ } if i c + = uint(data [i] i ++ } if i< len(data){c + = uint(data [i] i ++ } a,b,c = mix(a, b,c) return c,nil } func mix(a,b,c uint)(uint,uint,uint){a - = b ; a - = c; a ^ =(c> 13); b - = c; b - = a; b ^ =(a<< 8); c - = a; c - = b; c ^ =(b> 13); a - = b; a - = c; a ^ =(c>> 12); b - = c; b - = a; b ^ =(a<< 16); c - = a; c - = b; c ^ =(b>> 5); a - = b; a - = c; a ^ =(c> 3); b - = c; b - = a; b ^ =(a<< 10); c - = a; c - = b; c ^ =(b> 15); return a,b,c } 解决方案在我的代码中的其他问题使我远离 gob 包,结果证明这是@nvcnvn建议的正确方式。关于如何解决这个问题的相关代码如下: package bloom import( encoding / gobbytes) func GetBytes(key interface {})([] byte,error){ var buf bytes.Buffer enc:= gob.NewEncoder(& buf) err:= enc.Encode(key) if err!= nil { return nil,err } return buf.Bytes(),nil } I'm trying to write a hash that will accept all datatypes. Once in the function, I handle the data as a byte array. I'm having trouble figuring out how to cast an arbitrary interface{} to a byte array.I tried using the binary package but it seemed to depend on the type of data passed in. One of the parameters of the Write() fn (docs) required knowing the byte order of the parameter.All datatype sizes are some multiple of a byte (even the bool), so this should be simple in theory.Code in question below, package bloomimport ( "encoding/gob" "bytes")// adapted from http://bretmulvey.com/hash/7.htmlfunc ComputeHash(key interface{}) (uint, error) { var buf bytes.Buffer enc := gob.NewEncoder(&buf) err := enc.Encode(key) if err != nil { return 0, err } data := buf.Bytes() var a, b, c uint a, b = 0x9e3779b9, 0x9e3779b9 c = 0; i := 0; for i = 0; i < len(data)-12; { a += uint(data[i+1] | data[i+2] << 8 | data[i+3] << 16 | data[i+4] << 24) i += 4 b += uint(data[i+1] | data[i+2] << 8 | data[i+3] << 16 | data[i+4] << 24) i += 4 c += uint(data[i+1] | data[i+2] << 8 | data[i+3] << 16 | data[i+4] << 24) a, b, c = mix(a, b, c); } c += uint(len(data)) if i < len(data) { a += uint(data[i]) i++ } if i < len(data) { a += uint(data[i] << 8) i++ } if i < len(data) { a += uint(data[i] << 16) i++ } if i < len(data) { a += uint(data[i] << 24) i++ } if i < len(data) { b += uint(data[i]) i++ } if i < len(data) { b += uint(data[i] << 8) i++ } if i < len(data) { b += uint(data[i] << 16) i++ } if i < len(data) { b += uint(data[i] << 24) i++ } if i < len(data) { c += uint(data[i] << 8) i++ } if i < len(data) { c += uint(data[i] << 16) i++ } if i < len(data) { c += uint(data[i] << 24) i++ } a, b, c = mix(a, b, c) return c, nil}func mix(a, b, c uint) (uint, uint, uint){ a -= b; a -= c; a ^= (c>>13); b -= c; b -= a; b ^= (a<<8); c -= a; c -= b; c ^= (b>>13); a -= b; a -= c; a ^= (c>>12); b -= c; b -= a; b ^= (a<<16); c -= a; c -= b; c ^= (b>>5); a -= b; a -= c; a ^= (c>>3); b -= c; b -= a; b ^= (a<<10); c -= a; c -= b; c ^= (b>>15); return a, b, c} 解决方案 Other problems in my code led me away from the gob package earlier, turns out it was the proper way as @nvcnvn suggested. Relevant code on how to solve this issue below:package bloomimport ( "encoding/gob" "bytes")func GetBytes(key interface{}) ([]byte, error) { var buf bytes.Buffer enc := gob.NewEncoder(&buf) err := enc.Encode(key) if err != nil { return nil, err } return buf.Bytes(), nil} 这篇关于将任意Golang接口转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 18:00