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
Consider adding a function to the reflect package for creating a type by it's name:
// `TypeByName` creates a Type given its name.// It panics if the result type is not reachable.funcTypeByName(namestring) Type
This can e.g. be useful with generated code where the go generate directive contains information about the required types. Having TypeByName allows the generate implementation to reason about the types without having to hard-code them.
This allows the linker to aggressively remove dead code, making binaries smaller. Otherwise, when you import a package, all of the code in that package would have to be included in the final binary, because the linker does not know what will be requested at run time.
I do feel that the panic adequately explains that linker can proceed as needed. This would only give access to the types that already are included. But maybe this is too clumsy or awkward?
Proposal Details
Consider adding a function to the reflect package for creating a type by it's name:
This can e.g. be useful with generated code where the
go generate
directive contains information about the required types. HavingTypeByName
allows thegenerate
implementation to reason about the types without having to hard-code them.Example:
To work around
generate
(or any other package using this function) would still need to (_
) import the type's packages.Note: this proposal is equivalent to the older #21754 but adds a use case.
The text was updated successfully, but these errors were encountered: