-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: reflect: add GetTypeByName function #21754
Comments
@niubaoshu thank you for the proposal. However, would you mind giving more background to:
We try to carefully and pragmatically figure out what we should be added to the standard library, as APIs in here once added are forever as per the Go1.X compatibility promise, and also if something can be implemented without adding it as an official API. |
What happens if there are two packages named But I second @odeke-em's questions: what problem are you trying to solve with this? |
我在写一个序列化的程序,我需要序列化各种类型的值然后发送到服务端,服务端需要反序列化成一个对象。
server
我需要知道obj的具体的reflect.Type,才可以实现decode函数。
使用xxx包的人根本就不知道为什么要注册类型。 如果我们可以用一个string类型的字符串就可以创造出各种类型,这将带来很多的可能性。可以说是一生万物了。 写的不好,请见谅。 |
Lookup by name is a problem anyway. What happens if the same package is vendored into the binary under two different import paths? Then the serialization package will not know which one to use. We have used registration in a couple of different packages (such as (See also golang/protobuf#268.) |
This proposal conflicts with an important design goal of the reflect package: it is impossible to dynamically lookup a top-level package symbol. 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'm going to close this issue because I don't see a way the proposal can be reworked to be compatible with dead code elimination. If I'm wrong we can reopen it. |
proposal: add a func GetTypeByName(name string)(reflect.Type,error) in reflect package。
这是有必要的。请认真考虑。
call _,typ := GetTypeByName("os.File")
fmt.Println(typ,typ.Kind) // os.File ,struct
The text was updated successfully, but these errors were encountered: