New to Go and learning writing a REST library

blov · · 469 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Recently I started learning Go, and I was curious if someone could take a quick look through my repo and suggest me Go idioms that I&#39;m missing out on, parts of code that are not readable, imporoper approach if followed etc. </p> <p>I have try to follow below guidelines for learning and practicing</p> <h2>Learning objective and Requirements</h2> <ul> <li>Mockable database</li> <li>Sharing of all global configuration parameters across functions logger db if needed context for request scope</li> <li>Provide handler function to library client</li> <li>Dependency injection</li> <li>App Handler pattern</li> <li>Follow REST style</li> <li>Logging style and error handling</li> <li>To make idiomatic small libaries like user mgmt, login and similar repetetive tasks used in mostly every web project.</li> </ul> <h2>Assumptions and limitations</h2> <ul> <li>Targeting only sqlite library </li> <li>Made Dependencies on upper.io for database access, didn&#39;t wanted to spend time on writing sql queries. I understand Go practices suggest sql/sqlx. logrus logger for logging needs, wanted to explore logrus.</li> </ul> <h2>Things finding difficult to do.</h2> <ul> <li>Passing logger down to data access layer code (store functions). There are ways like passing through context or explicit argument. Not sure what is suitable approach.</li> </ul> <p>Would be glad if someone could provide a detailed review of my repo. Here is the repo link <a href="https://github.com/nkumar15/usermgmt" rel="nofollow">https://github.com/nkumar15/usermgmt</a></p> <p>Thank you.</p> <hr/>**评论:**<br/><br/>Mikojan: <pre><p>Not sure, maybe you did this on purpose, but you&#39;re missing out completely on Golang&#39;s world class support for testing. In the standard library there&#39;s even a package for integration testing web apps, <code>net/http/httptest</code>.</p> <p>Don&#39;t start a web server and check everything manually.</p> <p>Other than that in my mind you&#39;re relying (too?) heavily on external packages to do stuff that&#39;s rather simple.</p> <p>Like, logrus and negroni, when you&#39;re using gorilla and middleware is written quite easily by hand?</p> <p>IANAL, though! Keep it up!</p></pre>nkumar15: <pre><p>Thanks @Mikojan for looking at my code. For testing..agree I missed it. I have written some test cases but didn&#39;t commited it. I will add it.</p> <p>I am not sure, but i found over the internet many people prefer to use negroni middleware and in this case I found it easily fulfilling my request id need. However yes I agree when i want to write my own middleware, then I need to restrict it to negroni interfaces.</p> <p>For logging..any suggestions to make interface for any type of logger?</p></pre>: <pre><p>[deleted]</p></pre>Mikojan: <pre><p>you wouldnt understand...</p></pre>dchapes: <pre><p>s/Golang/Go</p></pre>Mikojan: <pre><p><a href="https://en.wikipedia.org/wiki/Go_(programming_language)" rel="nofollow">https://en.wikipedia.org/wiki/Go_(programming_language)</a></p></pre>WikiTextBot: <pre><p><strong>Go (programming language)</strong></p> <p>Go (often referred to as golang) is a free and open source programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a compiled, statically typed language in the tradition of Algol and C, with garbage collection, limited structural typing, memory safety features and CSP-style concurrent programming features added.</p> <hr/> <p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&amp;message=Excludeme&amp;subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>]</sup> <sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.24</sup></p></pre>

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

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