Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is there a way to change between types? #17204

Closed
Aukstkalnis opened this issue Sep 23, 2016 · 1 comment
Closed

Question: Is there a way to change between types? #17204

Aukstkalnis opened this issue Sep 23, 2016 · 1 comment

Comments

@Aukstkalnis
Copy link

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.

@ALTree
Copy link
Member

ALTree commented Sep 23, 2016

Hi! The Go project does not uses the github issue tracker for asking questions or general discussion about the language.

See the Questions wiki page for a list of places where you could post questions like this one.

@golang golang locked and limited conversation to collaborators Sep 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants