What is the best way to paginate in golang?

agolangf · · 983 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am new to golang and have developed an API using Gin that is going to be used in an android app which has unlimited scrolling, so need some sort of pagination to support that. The database is mariadb (connected through sqlx) So I&#39;m wondering what&#39;s the best way to do so? Is it better to use a package? Which one? If not are there any tutorials to teach pagination?</p> <hr/>**评论:**<br/><br/>tv64738: <pre><p>The same as in any other programming language. <a href="http://use-the-index-luke.com/no-offset" rel="nofollow">http://use-the-index-luke.com/no-offset</a></p></pre>gar44: <pre><p>I&#39;m spoiled by Django. I don&#39;t know how it is done in other languages. </p></pre>chrj: <pre><p>Here&#39;s some thoughts from Google&#39;s API design guidelines: <a href="https://cloud.google.com/apis/design/design_patterns#list_pagination" rel="nofollow">https://cloud.google.com/apis/design/design_patterns#list_pagination</a></p></pre>everdev: <pre><p>Pass a URL param like: mysite.com/search?q=myquery&amp;page=2</p> <p>Some sites also include a numResults param which can be used to increase the number of results.</p> <p>In your handler, query the DB with an offset of numResults * (page-1)</p></pre>gar44: <pre><p>There are some advice against using offsets, as explained in <a href="http://use-the-index-luke.com/no-offset" rel="nofollow">http://use-the-index-luke.com/no-offset</a></p></pre>cittatva: <pre><p>I think most gophers would say to write this yourself. The less you depend on the better, generally. Take a look at some popular API&#39;s for ideas about how to implement. Twitter API e.g. </p></pre>jeremiahs_bullfrog: <pre><p>Yup, <a href="https://stackoverflow.com/questions/3799193/mysql-data-best-way-to-implement-paging" rel="nofollow">here&#39;s an example of how to paginate in MySQL</a> (should be applicable to MariaDB or nearly any SQL database for that matter).</p></pre>gar44: <pre><p>Thanks for the tip. Actually I just followed the tips and modifed my queries to reflect that, so now I have queries like :</p> <p>err = Dbmap.Select(&amp;result, &#34;SELECT * FROM comment WHERE post_id = ? ORDER BY created_at ASC LIMIT ?,?&#34;, pId, offset, numResults)</p> <p>This does the job, however, regarding that using offsets is discouraged for performance reasons, I&#39;m not sure this is the optimal solution. </p></pre>jeremiahs_bullfrog: <pre><p>if you have a sequential date, you could also use <code>WHERE created_at &gt; ?</code>so whatever, though I&#39;m not sure on the performance differences. I say do it the way you have it and change it if it becomes a bottleneck. To me, this is the simplest, most obvious way to do it, so only change it if you have a good reason to.</p></pre>shovelpost: <pre><blockquote> <p>regarding that using offsets is discouraged for performance reasons</p> </blockquote> <p>I wouldn&#39;t say that offset is discouraged. It is just different tools for different jobs. Offset is good if you need to implement pagination that requires to go to a specific page. Offset is not that good if you want to implement an infinite scroller.</p></pre>gar44: <pre><p>Yeah, I&#39;ve seen a lot of these generic &#39;go figure out how to do it yourself&#39; in go community. I think it&#39;s simple arrogance and discouraging newbies, not help. </p></pre>circuitously: <pre><p>They&#39;re not saying you have to work it out yourself, they&#39;re saying you should build something as opposed to using a pagination package. You&#39;ll still get suggestions on how to approach it.</p></pre>xiamk: <pre><p>Hey @gar44, I know you&#39;re using sqlx but maybe you could consider upper-db for your next project, this is how&#39;d you do pagination: <a href="https://tour.upper.io/queries/04" rel="nofollow">https://tour.upper.io/queries/04</a>, you can create a simple LIMIT/OFFSET pagination or a cursor based one. </p></pre>

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

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