You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does this issue reproduce with the latest release?
yes
What did you do?
//Model is associated with the databasetypeModelstruct {
IDstringCreatedAt time.TimeUpdatedAt time.TimeDeletedAt*time.TimeVersionint
}
typeModelerinterface{
ID() stringSetID(string)
Version() intSetVersion(int)
}
funcSomeFunctionUseModeler(Modeler)
when the SomeFunctionUseModeler wants to use the Modeler interface.
In other languages,
We can use model if it implements the Modeler interface.
But in go, we can't do this.
Because the Model does not implement all the interfaces of the Modeler
If you want to use Model as Modeler,
You must change your ID and Version to other name.
The interface is widely used.
I think it should be more fault tolerant.
so if inheriting an interface,
If you use the same name and type of value as the interface, it is also considered to be its implementation.
Is it better?
The text was updated successfully, but these errors were encountered:
If you find it a lot of work to write the ModelModeler struct and methods, it's quite easy to write a code generator with 'go/ast' and go generate that will do it for you.
I'm not sure, but I think you are suggesting that if a struct field is named F with type T, then it should automatically satisfy an interface with a method F() T. If you want to write that up as a language change proposal, please see https://golang.org/s/proposal. But I don't think such a change is particularly likely to be accepted. Any type can have methods, so why should struct types be special in this way? Also, in general interfaces describe behavior, but a field is just data. And, of course, people will immediately want some way to change the field, not just fetch it.
I'm going to close this issue, but please feel free to open a language change proposal.
@beoran thanks for your response.
Although there is more structure, but it can solve this problem. @ianlancetaylor
i only want to solve this problem.
beoran's response helps me.
What version of Go are you using (
go version
)?go 1.13
Does this issue reproduce with the latest release?
yes
What did you do?
when the
SomeFunctionUseModeler
wants to use theModeler
interface.In other languages,
We can use model if it implements the
Modeler
interface.But in go, we can't do this.
Because the
Model
does not implement all the interfaces of theModeler
If you want to use
Model
asModeler
,You must change your
ID
andVersion
to other name.The
interface
is widely used.I think it should be more fault tolerant.
so if inheriting an interface,
If you use the same name and type of value as the interface, it is also considered to be its implementation.
Is it better?
The text was updated successfully, but these errors were encountered: