I wish there is an efficient way to print out my format. As I know convert to string may occur performance issue. Is there any better method?
package main
import "fmt"
type T struct {
order_no [5]byte
qty int32
}
func (t T)String() string {
return fmt.Sprint("order_no=", t.order_no,
"qty=", t.qty)
}
func main() {
v := T{[5]byte{'A','0','0','0','1'}, 100}
fmt.Println(v)
}
The output is order_no=[65 48 48 48 49]qty=100
I wish it will be order_no=A0001 qty=100
.
BTW, why func (t T)String() string
work and func (t *T)String() string
can not work.(on goplay)
package main
import "fmt"
type T struct {
order_no [5]byte
qty int32
}
func (t T) String() string {
return fmt.Sprint(
"order_no=", string(t.order_no[:]),
" qty=", t.qty,
)
}
func main() {
v := T{[5]byte{'A', '0', '0', '0', '1'}, 100}
fmt.Println(v)
}
Output:
order_no=A0001 qty=100