Golang: How to printf % x for bytes in a struct?

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

Golang: How to printf % x for bytes in a struct?

问题

以下是翻译的内容:

  1. var b [88]byte
  2. n, err := file.Read(b[:])
  3. fmt.Printf("读取的字节数:%d 字节:[% x]\n", n, b)
  4. 上述代码以十六进制打印字节
  5. 我有一个如下的结构体
  6. type SomeStruct struct {
  7. field1 []byte
  8. field2 []byte
  9. }
  10. someStructInstance := SomeStruct{[249 190 180 217], [29 1 0 0]}
  11. fmt.Println(someStructInstance)
  12. => {[249 190 180 217] [29 1 0 0]}
  13. 但是我希望它以十六进制打印
  14. => {[f9 be b4 d9] [1d 01 00 00]}
  15. 我该如何实现这个目标
英文:
  1. var b [88]byte
  2. n, err := file.Read(b[:])
  3. fmt.Printf("bytes read: %d Bytes: [% x]\n", n, b)

The above prints bytes in hexdecimal

I have a struct like this

  1. type SomeStruct struct {
  2. field1 []byte
  3. field2 []byte
  4. }
  5. someStructInstance := SomeStruct{[249 190 180 217], [29 1 0 0]}
  6. fmt.Println(someStructInstance)
  7. => {[249 190 180 217] [29 1 0 0]}

But ideally I would like it to print hexdecimal

  1. => {[f9 be b4 d9] [1d 01 00 00]}

How would I go about that?

答案1

得分: 5

我认为你只需要在SomeStruct上定义自己的String函数。这是一个示例:

  1. package main
  2. import "fmt"
  3. type SomeStruct struct {
  4. field1 []byte
  5. field2 []byte
  6. }
  7. func (s SomeStruct) String() string {
  8. return fmt.Sprintf("{[% x] [% x]}", s.field1, s.field2)
  9. }
  10. func main() {
  11. someStructInstance := SomeStruct{[]byte{249, 190, 180, 217}, []byte{29, 1, 0, 0}}
  12. fmt.Println(someStructInstance)
  13. }

在Go Playground上运行它:http://play.golang.org/p/eYBa1n33a2

英文:

I think you will just have to define your own String function on SomeStruct. Here is an example:

  1. package main
  2. import "fmt"
  3. type SomeStruct struct {
  4. field1 []byte
  5. field2 []byte
  6. }
  7. func (s SomeStruct) String() string {
  8. return fmt.Sprintf("{[% x] [% x]}", s.field1, s.field2)
  9. }
  10. func main() {
  11. someStructInstance := SomeStruct{[]byte{249, 190, 180, 217}, []byte{29, 1, 0, 0}}
  12. fmt.Println(someStructInstance)
  13. }

See it in running on the Go Playground: http://play.golang.org/p/eYBa1n33a2

答案2

得分: 3

你可以使用反射来检查结构体并打印其中的任何[]byte

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type SomeStruct struct {
  7. field1 []byte
  8. field2 []byte
  9. }
  10. type OtherStruct struct {
  11. intValue int
  12. intSlice []int
  13. byteSlice []byte
  14. }
  15. var typeOfBytes = reflect.TypeOf([]byte(nil))
  16. func printSlicesHex(obj interface{}) {
  17. value := reflect.ValueOf(obj)
  18. typeOfObj := value.Type()
  19. for i := 0; i < value.NumField(); i++ {
  20. field := value.Field(i)
  21. if field.Type() == typeOfBytes {
  22. bytes := field.Bytes()
  23. printBytes(typeOfObj.Field(i).Name, bytes)
  24. }
  25. }
  26. }
  27. func printBytes(name string, bytes []byte) {
  28. fmt.Printf("%s: [% x]\n", name, bytes)
  29. }
  30. func main() {
  31. someStructInstance := SomeStruct{[]byte{249, 190, 180, 217}, []byte{29, 1, 0, 0}}
  32. fmt.Println("Printing []bytes in SomeStruct")
  33. printSlicesHex(someStructInstance)
  34. fmt.Println()
  35. otherStruct := OtherStruct{0, []int{0, 1, 2}, []byte{0, 1, 2, 3}}
  36. fmt.Println("Printing []bytes in OtherStruct")
  37. printSlicesHex(otherStruct)
  38. }

这个示例会打印每个[]byte字段的名称和数据(以十六进制形式)。你可以通过传入一个自定义函数来改进它,以便不总是以十六进制形式打印。

Playground链接

英文:

You could use reflection to inspect the struct and print any []bytes that it has.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;reflect&quot;
  5. )
  6. type SomeStruct struct {
  7. field1 []byte
  8. field2 []byte
  9. }
  10. type OtherStruct struct {
  11. intValue int
  12. intSlice []int
  13. byteSlice []byte
  14. }
  15. var typeOfBytes = reflect.TypeOf([]byte(nil))
  16. func printSlicesHex(obj interface{}) {
  17. value := reflect.ValueOf(obj)
  18. typeOfObj := value.Type()
  19. for i := 0; i &lt; value.NumField(); i++ {
  20. field := value.Field(i)
  21. if field.Type() == typeOfBytes {
  22. bytes := field.Bytes()
  23. printBytes(typeOfObj.Field(i).Name, bytes)
  24. }
  25. }
  26. }
  27. func printBytes(name string, bytes []byte) {
  28. fmt.Printf(&quot;%s: [% x]\n&quot;, name, bytes)
  29. }
  30. func main() {
  31. someStructInstance := SomeStruct{[]byte{249, 190, 180, 217}, []byte{29, 1, 0, 0}}
  32. fmt.Println(&quot;Printing []bytes in SomeStruct&quot;)
  33. printSlicesHex(someStructInstance)
  34. fmt.Println()
  35. otherStruct := OtherStruct{0, []int{0, 1, 2}, []byte{0, 1, 2, 3}}
  36. fmt.Println(&quot;Printing []bytes in OtherStruct&quot;)
  37. printSlicesHex(otherStruct)
  38. }

For each []byte, this example prints the name of the field and its data (in hex). You could improve on this by taking a custom function to do the printing, so you don't always have to print in hex.

Playground Link

huangapple
  • 本文由 发表于 2014年1月6日 06:23:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/20939998.html
匿名

发表评论

匿名网友

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

确定