More idiomatic way in Go to encode a []byte slice int an int64?

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

More idiomatic way in Go to encode a []byte slice int an int64?

问题

在Go语言中,将[]byte切片编码为int64的更好或更惯用的方法是什么?

  1. package main
  2. import "fmt"
  3. func main() {
  4. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  5. var data int64
  6. for i := 0; i < 8; i++ {
  7. data |= int64(mySlice[i] & byte(255)) << uint((8*8)-((i+1)*8))
  8. }
  9. fmt.Println(data)
  10. }

链接:http://play.golang.org/p/VjaqeFkgBX

英文:

Is there a better or more idiomatic way in Go to encode a []byte slice into an int64?

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  5. var data int64
  6. for i := 0; i &lt; 8; i++ {
  7. data |= int64(mySlice[i] &amp; byte(255)) &lt;&lt; uint((8*8)-((i+1)*8))
  8. }
  9. fmt.Println(data)
  10. }

http://play.golang.org/p/VjaqeFkgBX

答案1

得分: 7

这是一小段代码,通过能够准确地看到正在发生的事情,可以获得一些清晰度。但这是一个非常有争议的观点,所以你自己的品味和判断可能会有所不同。

  1. func main() {
  2. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  3. data := int64(0)
  4. for _, b := range mySlice {
  5. data = (data << 8) | int64(b)
  6. }
  7. fmt.Printf("%d\n", data)
  8. }

输出结果为:

  1. -795741901218843404

Playground链接: https://go.dev/play/p/aemkEg7a6S5

英文:

It's such a tiny amount of code, there's some clarity gained by being able to see exactly what's going on. But this is a highly contentious opinion, so your own taste and judgement may differ.

  1. func main() {
  2. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  3. data := int64(0)
  4. for _, b := range mySlice {
  5. data = (data &lt;&lt; 8) | int64(b)
  6. }
  7. fmt.Printf(&quot;%d\n&quot;, data)
  8. }

Prints:

  1. -795741901218843404

Playground: https://go.dev/play/p/aemkEg7a6S5

答案2

得分: 1

我不确定是否符合惯用语,但这里有一个使用encoding/binary包的替代方案:

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. )
  7. func main() {
  8. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  9. buf := bytes.NewReader(mySlice)
  10. var data int64
  11. err := binary.Read(buf, binary.LittleEndian, &data)
  12. if err != nil {
  13. fmt.Println("binary.Read failed:", err)
  14. }
  15. fmt.Println(data)
  16. }

这里是一个可以运行的示例链接。

英文:

I'm not sure about idiomatic, but here's an alternative using the encoding/binary package:

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;encoding/binary&quot;
  5. &quot;fmt&quot;
  6. )
  7. func main() {
  8. var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
  9. buf := bytes.NewReader(mySlice)
  10. var data int64
  11. err := binary.Read(buf, binary.LittleEndian, &amp;data)
  12. if err != nil {
  13. fmt.Println(&quot;binary.Read failed:&quot;, err)
  14. }
  15. fmt.Println(data)
  16. }

http://play.golang.org/p/MTyy5gIEp5

huangapple
  • 本文由 发表于 2014年1月2日 03:09:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/20872173.html
匿名

发表评论

匿名网友

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

确定