Separation of code using package or functions in different files?

xuanbao · · 979 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey guys, I have created different packages for separation of my code. Most of my functions which will be called through http router moves to package controller, where we have function to CreateUser(), GetUser(), CreateStatus(), UpdateStatus(), GetStatus() etc. I have moved all user related code in user.go and status related code in status.go, both in same package. As they are in same package, any function can call any other function. Want to know is this right way to do or should i create subpackage for user, status and more things which will come. My issue with subpackage is that there will be many subpackages each for one category like user, status, friend etc and each subpackage need different folder.</p> <hr/>**评论:**<br/><br/>printf_hello_world: <pre><p>I find that it can get a bit unwieldy to have separate packages for each thing that we might call a &#34;class&#34; in some other languages.</p> <p>Maybe it&#39;s a good time to evaluate your reasoning for the level of scoping you&#39;re talking about. Are you:</p> <ul> <li>Collaborating on this code with many (possibly unfamiliar) developers?</li> <li>Worried about the level of complexity within user.go and status.go?</li> <li>Concerned that future maintainers may use/abuse package-level functionality in unintended or dangerous ways?</li> </ul> <p>Then maybe you should make subpackages. Otherwise, you&#39;re just making a tradeoff between simplicity (one package) and information-theoretic purity (many packages).</p></pre>myth007: <pre><p>Thanks for clarifying with detailed explanation. I think i would like to go with subpackages as my requirement suits that and that will give me more separation of responsibility in code.</p></pre>printf_hello_world: <pre><p>Cool.</p> <p>As a bonus, now you can make spiffy-named functions like user.Create() and and status.Create(), possibly replacing (or else being wrapped by) controller.CreateUser() and controller.CreateStatus()</p></pre>myth007: <pre><p>yes, it seems more like OOP to me .. using packages like static classes.</p></pre>jameycribbs: <pre><p>I ended putting each of my controllers in it&#39;s own package. That way I could name my controller methods Index(), New(), Create(), etc. in each controller and did not have to name them UserIndex(), etc. To me it just made more sense.</p> <p>But it&#39;s really up to you.</p></pre>myth007: <pre><p>Thanks a lot. You gave me a bit of confidence about my thinking. Most difficult part is writing function name, really like your simple and efficient names :).</p></pre>divan0: <pre><p>Note, that appengine doesn&#39;t like using subpackages (it can create name collisions). Only if you have each subpackage in your project as a module.</p></pre>myth007: <pre><p>agreed. In line of go, i want to make my code more structured so that anyone comes and work on my code and easily get how the code is structured in different submodules based on its type like user in user submodule, status in status submodule all belonging to controller module. Any better suggestion on how we can achieve desired results?</p></pre>divan0: <pre><p>well, that was a reason why I moved out of appengine in a recent project :/</p></pre>

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

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