Asserting type based off text?

blov · · 366 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>If I have the following code:</p> <pre><code>func main() { t := MyType{&#34;Nate&#34;} 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&#39;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&#39;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 &#39;somepackage.SomeType&#39;? 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[&#34;somepackage.SomeType&#34;] = func() interface{} { return &amp;SomeType{} } st := Types[&#34;somepackage.SomeType&#34;]() </code></pre> <p>You&#39;d then need to make interfaces and implement them on SomeType to get &amp; 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&#39;ll see if there&#39;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&#39;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[&#34;Nate&#34;] </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{ &#34;pkga&#34;: pkga.Types, &#34;pkgb&#34;: 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 &#34;You Don&#39;t Need Permission To Use Class Methods&#34; section</a>. I&#39;m not 100% sure, because it doesn&#39;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&#39;d try to simply write to some interface if possible.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

366 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传