-
Notifications
You must be signed in to change notification settings - Fork 50
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
Feature: Implement data type comparison #161
Comments
A little while ago I considered adding something like Ex: asCustomType, isType1 := i.(CustomType1)
assert.Assert(t, isType1)
asCustomType.Foo() Pure type assertions didn't seem very useful because the compiler will already check the type for you. Do you have a more concrete example of where you would want to use this comparison but not use the type afterward? |
Well, I implemented an interface and I am testing if the returned driver is correct type: type (
DataSource struct {
Adapter string
}
DriverInterface interface {
BlockList() []string
}
YamlDriver struct { }
XmlDriver struct { }
)
func (yamlDriver *YamlDriver) BlockList() []string {
...
}
func (xmlDriver *XmlDriver) BlockList() []string {
...
}
func BuildDriver(DataSource ds) {
...
} func TestBuildDriverYaml(t *testing.T) {
var ds = NewDataSource()
ds.Adapter = "yaml"
var i interface{} = BuildDriver(ds)
_, isYamlDriver := i.(*YamlDriver)
assert.Assert(t, isYamlDriver)
} |
You can have the compiler perform that test for you by adding this line to a package: var _ DriverInterface = &YamlDriver{} I believe that's the recommended practice for checking a type implements an interface. |
A better example from #147 doer, ok := something.(Doer)
assert.Assert(t, ok, "got: %T", something) var _ DriverInterface = (*YamlDriver)(nil) |
In order to compare between data types, I am doing
It would be better if
The text was updated successfully, but these errors were encountered: