Hi, I'd need some help with a design problem.

xuanbao · · 405 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello guys,<br/> I&#39;m writing a program to generate input for <a href="https://github.com/LemonBoy/bar">lemonbar</a>, using json files as config.<br/> My idea is that I&#39;ll have different types of modules (or blocks) like username, date, clock ecc...<br/> How I&#39;m handling this right now is that I have only one struct called Module which has a Handler variable that is assigned to a different functions based on what the json file says. I don&#39;t really like this solution, because I can&#39;t keep things like per-module options separated, but I would love to hear your opinion on that. I think a nice way to do it would be having separate structs for each module type, but I couldn&#39;t figure out how to implement it.<br/> You can find the code <a href="https://github.com/guglicap/golem/blob/develop/modules/module.go">here</a>.<br/> Thanks. </p> <hr/>**评论:**<br/><br/>beknowly: <pre><p><code>Module</code> can be an interface if you want modules that are used the same way but have implementation specific state.</p> <pre><code>type Module interface { Run(msg chan bool) // whatever other required methods } type NetModule struct { // whatever state you need in a NetModule } func (n NetModule) Run(msg chan bool) { // do whatever this module does for { time.Sleep(1 * time.Second) msg &lt;- true } } </code></pre> <p>Then you can instantiate say, a slice of <code>Module</code>s, call <code>Run(mychannel)</code> on each in a goroutine, and handle incoming events on <code>mychannel</code>.</p></pre>guglicap: <pre><p>Thanks for the reply.<br/> That would be the solution I thought of initially, but I couldn&#39;t figure out how to implement, mostly because of JSON.<br/> Most of these modules have the same properties, say<br/> <code>{ &#34;Handler&#34;:&#34;net&#34;, &#34;Refresh&#34;:&#34;3s&#34;, ... &#34;Options&#34;:{ What differs from module to module is this part, as I&#39;ll have say NetInterface as an options for this module but Format as an option for the date one. }</code><br/> Thus, how can I know what to unmarshal it into? I don&#39;t think I can unmarshal it into []Module, since Module is an interface. </p> <p>EDIT: By the way, here&#39;s <a href="https://github.com/guglicap/golem/blob/develop/config.json" rel="nofollow">an example config file</a>, since I&#39;m on mobile and the example I typed is probably not well formatted. </p></pre>xuoe: <pre><p>You could implement the <code>json.Unmarshaler</code> interface on the <code>Config</code> struct and use <a href="https://golang.org/pkg/encoding/json/#RawMessage" rel="nofollow"><code>json.RawMessage</code></a> for module-specific options. That might work. Have a look <a href="https://play.golang.org/p/ywhTDErAka" rel="nofollow">here</a>, where I took some of your code and applied those ideas (including, to some extent, what <a href="/u/beknowly" rel="nofollow">/u/beknowly</a> suggested).</p> <p>EDIT 1: in the code snippet, creating a <code>json.Decoder</code> is not necessary (line 51), as a call to <code>json.Unmarshal(data, &amp;conf)</code> would suffice there.</p> <p>EDIT 2: here&#39;s, what I hope to be, <a href="https://play.golang.org/p/QhbpVOHJf_" rel="nofollow">an improved version</a>.</p></pre>guglicap: <pre><p>Thanks a lot, that should do. </p></pre>

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

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