Skip to content

Instantly share code, notes, and snippets.

@AntonStoeckl
Last active January 14, 2022 20:10
Show Gist options
  • Save AntonStoeckl/3a7edb09e62968ec2b9e158f604f7439 to your computer and use it in GitHub Desktop.
Save AntonStoeckl/3a7edb09e62968ec2b9e158f604f7439 to your computer and use it in GitHub Desktop.
Example for blog post Go bits: Interfaces and Nil Pointers
package commandhandling_test
import (
"testing"
"go-bits/interfaces-and-nil/commandhandling"
)
func TestNilInterface(_ *testing.T) {
var nilInterface commandhandling.Command
if nilInterface != nil {
_ = nilInterface.CommandType()
}
}
func TestNilPointerCommand(_ *testing.T) {
var nilCommand *commandhandling.RegisterCustomer
if nilCommand != nil {
_ = nilCommand.CommandType()
}
}
func TestInterfaceWithUnderlyingNilPointer(_ *testing.T) {
var nilCommand *commandhandling.RegisterCustomer
var interfaceWithUnderlyingNilPointer commandhandling.Command = nilCommand
//nolint:staticcheck // even the linter knows that this is always true ;-)
if interfaceWithUnderlyingNilPointer != nil {
_ = interfaceWithUnderlyingNilPointer.CommandType()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment