Best usage of reflect Kind and Type. Get Kind of custom struct?

polaris · · 804 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi <a href="/r/golang" rel="nofollow">/r/golang</a> </p> <p>I&#39;m working on a project where we are working with tuple and tuple spaces. In order to find not necessary an exact match, we have to do some matching with types.</p> <p>Right now when searching for tuple we create a template. The template contains fields of reflect.Type. That in itself is no problem, the problem is the declaration of it, as it looks something like this</p> <pre><code>var template = CreateTemplate(Reflect.TypeOf(&#34;string&#34;), Reflect.TypeOf(2)) </code></pre> <p>This code with create a template that will match with tuples where the first field is of type String and the second field is of type Int.</p> <p>Using only primitive types we could change this to Reflect.Kind. However, it&#39;s not unthinkable that a tuple could contain a struct as a field. Lets say we want to find tuples where the first field is of type Specification (a struct). With that we want to be able to create a template like</p> <pre><code>var template = CreateTemplate(Specification) </code></pre> <p>If we use reflect.TypeOf(Specification).Kind() = struct</p> <p>Is there a solution to this problem? It&#39;s not very nice that the user has to define a template as they have to right now, writing a random value of the type wanted for that field.</p> <p>Cheers</p>

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

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