Last active
January 14, 2022 20:10
-
-
Save AntonStoeckl/3a7edb09e62968ec2b9e158f604f7439 to your computer and use it in GitHub Desktop.
Example for blog post Go bits: Interfaces and Nil Pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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