英文:
How to pretty print byte array and char array?
问题
在下面的代码中,我试图编写一个Txt()函数来漂亮地打印出我的结构。在完整的代码中,包含以下几个小问题:
- 如何通过字符串初始化字符数组的一行代码(第47行)。
- 如何加快检查字符类型而不使用字符串函数(第29、30行)。
- 如何将字符数组打印为字符串(第32行)。
- 如何将字符打印为字符串,可能使用Sprintf("%c"),但它非常慢(第34行)。
完整代码请参考:http://play.golang.org/p/nUsg_qbufP
type Char byte
type THeader struct {
Ver int8 // 将显示为1
Tag Char // 将显示为'H'
}
type TBody struct {
B1 [3]byte // 将显示为"[0,0,0]"
B2 [4]Char // 将显示为"ABCD"
}
func Txt(t interface{}) (s string) {
val := reflect.ValueOf(t)
typ := val.Type()
fields := typ.NumField()
for i := 0; i < fields; i++ {
sf := typ.Field(i)
valfld := val.Field(i)
vType := valfld.Type()
s += sf.Name + ":" + vType.String() + ":"
if strings.HasSuffix(vType.String(), "Char") {
if strings.HasPrefix(vType.String(), "[") {
v, ok := valfld.Interface().([4]Char)
s += fmt.Sprint(ok, v) + "\n"
} else {
s += fmt.Sprint(valfld.Interface()) + "\n"
}
} else {
s += fmt.Sprint(valfld.Interface()) + "\n"
}
}
return
}
func main() {
th := THeader{1, 'H'}
fmt.Printf("%#v\n", th)
// tb := TBody{B2: [10]Char("ABCD")}
tb := TBody{B2: [4]Char{'A', 'B', 'C', 'D'}}
fmt.Printf("%#v\n", tb)
fmt.Print("Txt(th):\n", Txt(th), "\n")
fmt.Print("Txt(tb):\n", Txt(tb), "\n")
}
英文:
In the following code, I'm trying to write a Txt() function to pretty print out my structure. It contains following minor questions in the full code:
- How to write one line to initialize Char array by string(line 47)
- How to speed up checking Char type without strings function(line 29,30)
- How to print out Char array as string(line 32)
- How to print out Char as string, maybe use Sprintf("%c"), but it is very slow.(line 34)
full code at: http://play.golang.org/p/nUsg_qbufP
type Char byte
type THeader struct {
Ver int8 // will show 1
Tag Char // will show 'H'
}
type TBody struct {
B1 [3]byte // will show "[0,0,0]"
B2 [4]Char // will show "ABCD"
}
func Txt(t interface{}) (s string) {
val := reflect.ValueOf(t)
typ := val.Type()
fields := typ.NumField()
for i := 0; i < fields; i++ {
sf := typ.Field(i)
valfld := val.Field(i)
vType := valfld.Type()
s += sf.Name + ":" + vType.String() + ":"
if strings.HasSuffix(vType.String(), "Char") {
if strings.HasPrefix(vType.String(), "[") {
v, ok := valfld.Interface().([4]Char)
s += fmt.Sprint(ok, v) + "\n"
} else {
s += fmt.Sprint(valfld.Interface()) + "\n"
}
} else {
s += fmt.Sprint(valfld.Interface()) + "\n"
}
}
return
}
func main() {
th := THeader{1, 'H'}
fmt.Printf("%#v\n", th)
// tb := TBody{B2: [10]Char("ABCD")}
tb := TBody{B2: [4]Char{'A', 'B', 'C', 'D'}}
fmt.Printf("%#v\n", tb)
fmt.Print("Txt(th):\n", Txt(th), "\n")
fmt.Print("Txt(tb):\n", Txt(tb), "\n")
}
答案1
得分: 1
这段代码应该可以回答除了你的第一个问题之外的所有问题,因为函数无法返回长度不同的数组,而Go语言也没有初始化动态派生大小数组的功能。对于这些问题,你需要使用切片。其余的代码可以使用惯用的Go语言方式解决,使用Stringer接口而不需要反射。
package main
import (
"fmt"
)
type Char byte
type CharSlice []Char
type ByteSlice []byte
func (s CharSlice) String() string {
ret := ""
for _, b := range s {
ret += fmt.Sprintf("%c", b)
}
ret += ""
return ret
}
func (s ByteSlice) String() string {
return fmt.Sprintf("%v", []byte(s))
}
type THeader struct {
Ver int8 // will show 1
Tag Char // will show 'H'
}
func (t THeader) String() string {
return fmt.Sprintf("{ Ver: %d, Tag: %c}", t.Ver, t.Tag)
}
type TBody struct {
B1 [3]byte // will show "[0,0,0]"
B2 [4]Char // will show "ABCD"
}
func (t TBody) String() string {
return fmt.Sprintf("{ B1: %s, B2: %s", ByteSlice(t.B1[:]), CharSlice(t.B2[:]))
}
func main() {
th := THeader{1, 'H'}
fmt.Printf("%#v\n", th)
tb := TBody{B2: [4]Char{'A', 'B', 'C', 'D'}}
fmt.Printf("%#v\n", tb)
fmt.Printf("Txt(th):\n%s\n", th)
fmt.Printf("Txt(tb):\n%s\n", tb)
}
英文:
This code should answer all except for your 1'st questions which is impossible since a function can't return arrays of varying length and Go has no facility to initialize an array of dynamically derived sizes. You need slices for those. The rest of the code is solveable using idiomatic go with the Stringer interface and no reflection required.
package main
import (
"fmt"
)
type Char byte
type CharSlice []Char
type ByteSlice []byte
func (s CharSlice) String() string {
ret := "\""
for _, b := range s {
ret += fmt.Sprintf("%c", b)
}
ret += "\""
return ret
}
func (s ByteSlice) String() string {
return fmt.Sprintf("%v", []byte(s))
}
type THeader struct {
Ver int8 // will show 1
Tag Char // will show 'H'
}
func (t THeader) String() string {
return fmt.Sprintf("{ Ver: %d, Tag: %c}", t.Ver, t.Tag)
}
type TBody struct {
B1 [3]byte // will show "[0,0,0]"
B2 [4]Char // will show "ABCD"
}
func (t TBody) String() string {
return fmt.Sprintf("{ B1: %s, B2: %s", ByteSlice(t.B1[:]), CharSlice(t.B2[:]))
}
func main() {
th := THeader{1, 'H'}
fmt.Printf("%#v\n", th)
tb := TBody{B2: [4]Char{'A', 'B', 'C', 'D'}}
fmt.Printf("%#v\n", tb)
fmt.Printf("Txt(th):\n%s\n", th)
fmt.Printf("Txt(tb):\n%s\n", tb)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论