Gohper v0.1 released with much changes - common libs for gophers

xuanbao · · 864 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/cosiner/gohper">github.com/cosiner/gohper</a>.</p> <p>The gohper package‘s structure has been fully changed, and some modules was moved to <a href="https://github.com/cosiner/ygo">github.com/cosiner/ygo</a>.</p> <p>If some guys are using gohper, i am sorry for these changes, but it&#39;s very worthwhile to get a more clearly package.</p> <p>The gohper package focus many libs i used in development, i am very pleased if it&#39;s helpful for community and gophers.</p> <p>Sorry for my China English, :).</p> <hr/>**评论:**<br/><br/>izuriel: <pre><p>The only comment I have at the moment is that your text functions should be using the Unicode package. Right now they&#39;re very dependent on ASCII encodings and sing byte characters which will probably work for most practical use cases but I think it wise to make string functions Unicode smart since runes are Unicode code points. </p></pre>tdewolff: <pre><p>Which functions do you mean? Generally, if it works for single byte characters it works for UTF-8 as well, because UTF-8 was designed with that in mind. See <a href="http://research.swtch.com/utf8" rel="nofollow">http://research.swtch.com/utf8</a></p></pre>izuriel: <pre><p>I know it can work. But take your snake case function. It only counts A-Z as uppercase. Instead, there is a Unicode method to query about the case of a code point. </p> <p>I never said it <em>wouldn&#39;t</em> work, I just said string functions in Go should be Unicode aware. </p></pre>daveddev: <pre><p>I wrote this a while back, and would appreciate criticisms: <a href="http://play.golang.org/p/JGIWzi5o6g" rel="nofollow">http://play.golang.org/p/JGIWzi5o6g</a></p></pre>izuriel: <pre><p>I don&#39;t see why you chose to build backwards, there is a lot of rebuilding going on with your <code>append([]rune{&#39;_&#39;}, buf...)</code>. Provided a sample string of:</p> <pre><code>&#34;SomeClass&#34; </code></pre> <p>You&#39;d end up with a (at least mental, optimizations my change this):</p> <pre><code>append([]rune{&#39;_&#39;}, &#39;C&#39;, &#39;l&#39;, &#39;a&#39;, &#39;s&#39;, &#39;s&#39;) </code></pre> <p>Which will continue to grow if I <code>FeedItSomeLongNameThatWillSlowThingsDown</code>.</p> <p>I, personally, would go with something along the lines of this:</p> <pre><code>func snake(s string) string { rs := []rune(s) lastLower := false buf := new(bytes.Buffer) for _, r := range rs { if unicode.IsLetter(r) { if lastLower &amp;&amp; unicode.IsUpper(r) { buf.WriteRune(&#39;_&#39;) lastLower = false } else { lastLower = true } } buf.WriteRune(r) } return strings.ToLower(buf.String()) } </code></pre> <p>(further example of unicode support vs. none can be found <a href="https://play.golang.org/p/jIInNxgjCC" rel="nofollow">here</a>)</p> <p>The real improvement here is just being more clear about what&#39;s going. By not iterating backwards and caching (via the <code>bool</code>) whether or not I&#39;d previously encountered a &#39;lowercase&#39; character I was able to reduce the if/else to a very simple branch test. Also looping forward has prevented the awkward need to append the current buffer to the end of a rune to get the new buffer. Using this method allows for <code>range</code> to work which also removes any direct array lookups from happening throughout the body of the loop.</p> <p><strong>edit:</strong> modify sample and update link</p></pre>daveddev: <pre><p>This doesn&#39;t pass the test in my play. - <a href="http://play.golang.org/p/paOMLE_syv" rel="nofollow">http://play.golang.org/p/paOMLE_syv</a></p> <p>If I recall correctly, working backward helped to accommodate acronyms and single caps. I think I remember not being able to nail this down until I worked &#34;in reverse&#34;. However, it&#39;s been a while and I may be mistaken about my reasoning at that time.</p> <p>Your time posting this is greatly appreciated! I&#39;ll dig into your function in the morning and glean as much wisdom as I can.</p> <p>Edited to add additional test containing &#34;SQL&#34; - The only thing my version doesn&#39;t cover is when a single cap comes right before an acronym. I contemplated using a list of acronyms (<a href="https://github.com/golang/lint/blob/3d26dc39376c307203d3a221bada26816b3073cf/lint.go#L482" rel="nofollow">https://github.com/golang/lint/blob/3d26dc39376c307203d3a221bada26816b3073cf/lint.go#L482</a>) to aid in this issue, but wanted to keep the function light. And so far, it has not been a problem.</p> <p>As an aside, searching for the lint list of acronyms brought me across this lib - <a href="https://github.com/serenize/snaker" rel="nofollow">https://github.com/serenize/snaker</a> I&#39;ll look into that tomorrow as well, but I think I recall passing that over previously.</p></pre>izuriel: <pre><p>Yea I wasn&#39;t going for full on feature complete. That function doesn&#39;t handle any other form of spacing. I was just doing what you asked, providing criticisms and wanted to demonstrate the general method I would have employed with a simple example. </p> <p>I threw in the ASCII example to strengthen my argument before you came around, that wasn&#39;t necessarily directed at you.</p> <p>If I wasn&#39;t just giving criticisms, I think your&#39;e solution works - the only real issue I had with it was that it wasn&#39;t immediately clear what you were doing, I had to dig in. That doesn&#39;t signify anything bad necessarily - just something I pointed out!</p></pre>Darxide-: <pre><p>As someone still relatively new to Go, it looks like a bunch of these are just rewrites of whats in the standard library? Why, what benefits are gained in some of these libraries. </p></pre>cosiner_z: <pre><p>It&#39;s not intend on rewrites, but the convenience.<br/> Extract the common, helpful pattern, data structures, functions and components from code, make them a second base library, that&#39;s what i do: DRY! Don&#39;t repeat yourself!<br/> One can imagin how tedious it is to repeat yourself day by day!</p> <p>Anyone can take codes from it and put yours to it, welcome for feedbacks and contributions.</p></pre>

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

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