proposal: spec: Referencing structs field type in another field definition #69194
Closed
1 of 4 tasks
Labels
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
Proposal
Milestone
Go Programming Experience
Intermediate
Other Languages Experience
Javascript, Typescript, React/NextJS, PHP, C/C++
Related Idea
Has this idea, or one like it, been proposed before?
No
Does this affect error handling?
No
Is this about generics?
No
Proposal
Hello!
I would like to suggest a feature to the Go language.
It would be nice to be able to reference the field type of another model directly, similar to how TypeScript allows referencing types from other interfaces or classes.
I know I can define a type and use that type for it, but this would improve the process with less redundant code.
My situation:
Currently, when defining a model in GORM, I must explicitly specify the type of fields, including foreign keys, which can lead to inconsistencies and increased maintenance overhead when changing models etc.
In TS I can do this:
In the example, Order type references the type of Product's id property directly, ensuring that any changes to the Product interface will automatically reflect in the Order interface.
Thanks.
Language Spec Changes
Adding new way of defining struct type
Informal Change
No response
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
This feature will definitely simplify learning Go to (not only) Typescript users.
Cost Description
No response
Changes to Go ToolChain
I guess type check features, syntax highlighters.
Performance Costs
No runtime, comptime is affected like similiar to adding new type to the code
Prototype
To implement a similar feature in Go, I suggest introducing a syntax that allows referencing another model's field type directly. As Go uses object a dot for struct data, either use a keyword
typeof Model.Column
orModel['Column']
.For example:
Thanks for reading and feedback.
The text was updated successfully, but these errors were encountered: