You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type IO1b_t []struct {
ID uint8
Val uint8
}
type IO2b_t []struct {
ID uint8
Val uint16
}
type IO4b_t []struct {
ID uint8
Val uint32
}
type IO8b_t []struct {
ID uint8
Val uint64
}
I want to implement Stringer interface for both types. The body of String() is identical:
var str string
for _,value := range rec {
str += fmt.Sprintf("ID:%d, Value:%d")
}
return str
Is it possible to implement one function for both receivers? The example would be like this:
funct (io IO1b_t) String() string {
getIoFormatString(io)
}
funct (io IO2b_t) String() string {
getIoFormatString(io)
}
funct (io IO4b_t) String() string {
getIoFormatString(io)
}
funct (io IO8b_t) String() string {
getIoFormatString(io)
}
funst getIoFormatString(rec interface{}) string {
var str string
for _,value := range rec {
str += fmt.Sprintf("ID:%d, Value:%d")
}
return str
}
Is it posible to do someting like this? If so, how? Of corse, I can implement the some method body for all methods, but I thought that there has to be a better way. I would like to have only one fmt.Sprintf() function for efficiency.
Thanks.
The text was updated successfully, but these errors were encountered:
Hi,
I have 4 slices of structures:
type IO1b_t []struct {
ID uint8
Val uint8
}
type IO2b_t []struct {
ID uint8
Val uint16
}
type IO4b_t []struct {
ID uint8
Val uint32
}
type IO8b_t []struct {
ID uint8
Val uint64
}
I want to implement Stringer interface for both types. The body of String() is identical:
var str string
for _,value := range rec {
str += fmt.Sprintf("ID:%d, Value:%d")
}
return str
Is it possible to implement one function for both receivers? The example would be like this:
funct (io IO1b_t) String() string {
getIoFormatString(io)
}
funct (io IO2b_t) String() string {
getIoFormatString(io)
}
funct (io IO4b_t) String() string {
getIoFormatString(io)
}
funct (io IO8b_t) String() string {
getIoFormatString(io)
}
funst getIoFormatString(rec interface{}) string {
var str string
for _,value := range rec {
str += fmt.Sprintf("ID:%d, Value:%d")
}
return str
}
Is it posible to do someting like this? If so, how? Of corse, I can implement the some method body for all methods, but I thought that there has to be a better way. I would like to have only one fmt.Sprintf() function for efficiency.
Thanks.
The text was updated successfully, but these errors were encountered: