<p>If I have the following code:</p>
<pre><code>func main() {
t := MyType{"Nate"}
typ := reflect.TypeOf(t)
fmt.Println(typ)
}
type MyType struct {
Name string
}
</code></pre>
<p>And it returns <code>main.MyType</code>, is there any way to create a new type based on that returned string value?</p>
<p>What I'm trying to do is type-assert an interface value based on the returned text of <code>reflect.Type(T)</code>. So if I already KNOW what type it is based on some text (like <code>somepackage.SomeType</code>), can I then say that the interface I've accepted in some function is of type <code>somepackage.SomeType</code>?</p>
<hr/>**评论:**<br/><br/>nilslice: <pre><p>What are you trying to do with your 'somepackage.SomeType'? Could you provide an example? And is a type switch not something you can use in the function that accepts an interface{}?</p>
<p>Could you try keeping a map of the types you want to make new instances of based on the string name returned from reflect.TypeOf:</p>
<pre><code>var Types = make(map[string]func() interface{})
Types["somepackage.SomeType"] = func() interface{} { return &SomeType{} }
st := Types["somepackage.SomeType"]()
</code></pre>
<p>You'd then need to make interfaces and implement them on SomeType to get & set its values. </p></pre>hobbified: <pre><p>Use four-space indent on reddit, not ``` blocks.</p></pre>nilslice: <pre><p>Ah there it is. Thanks!</p></pre>natdm: <pre><p>Thanks guys. What I think I was trying to do was already pretty sketchy. I'll see if there's other idiomatic ways. </p></pre>VerilyAMonkey: <pre><p>If you are certain you know what type it is, you just need to get the value as an <code>interface{}</code> and then type assert it. Like so:</p>
<pre><code>typed := value.Interface().(MyType)
</code></pre>
<p>where <code>value</code> is a <code>reflect.Value</code>. Or minus the <code>.Interface()</code> if it's already an <code>interface{}</code>. </p></pre>cdoxsey: <pre><p>You can use the pkgreflect library: <a href="https://github.com/ungerik/pkgreflect" rel="nofollow">https://github.com/ungerik/pkgreflect</a></p>
<p>It generates maps of the types to make it easier to reflect on them:</p>
<pre><code>var Types = map[string]reflect.Type{ ... }
</code></pre>
<p>So:</p>
<pre><code>typ := Types["Nate"]
</code></pre>
<p>You could build another map of all the packages you want:</p>
<pre><code>var Packages = map[string]map[string]reflect.Type{
"pkga": pkga.Types,
"pkgb": pkgb.Types,
}
</code></pre></pre>jerf: <pre><p>You may be able to use what <a href="http://www.jerf.org/iri/post/2945" rel="nofollow">I talk about here, under the "You Don't Need Permission To Use Class Methods" section</a>. I'm not 100% sure, because it doesn't always work depending on exactly what you start with. But if you want to be able to go from a string name to a full object, you control all the objects, and can implement the methods, this will do.</p>
<p>From there you can do a normal non-reflection type assertion, but before that, I'd try to simply write to some interface if possible.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传