-
Notifications
You must be signed in to change notification settings - Fork 195
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
Allocless way to check interface compatibility #33
Comments
I see! Thank you so much for taking the time to point this out! 🙏 I actually thought that the compiler would optimise the I will write up a pull request for this 😄 Sorry for the late reply! |
@vbauerster I had a look at this, using the build flag package main
import (
"io"
)
type NullWriter struct{}
func (writer *NullWriter) Write(b []byte) (int, error) {
return 0, nil
}
var _ io.Writer = (*NullWriter)(nil)
var _ io.Writer = &NullWriter{}
func main() {
var _ io.Writer = &NullWriter{}
var _ io.Writer = (*NullWriter)(nil)
return
} This gave me the following output:
This suggests that the compiler actually optimises the Perhaps I am going about this all wrong though? If you have input on this, let me know, I would love to learn! Thank you 🙏 |
Frankly speaking I didn't think about |
Alright, I will keep the issue open, so that anyone can chime in and maybe give us a definitive answer on this 😄 Thanks again for opening the issue! |
Interfaces part shows a way to check interface compatibility (fulfillment):
Nothing wrong with above code, but for completeness sake it worth mention that we needn't allocate a new variable since any value of type
*NullWriter
will do, even nil:This explicit conversion works, because
nil
is typed in Go. Check this playground example.The text was updated successfully, but these errors were encountered: