<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'm wondering what'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'm spoiled by Django. I don't know how it is done in other languages. </p></pre>chrj: <pre><p>Here's some thoughts from Google'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&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'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'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(&result, "SELECT * FROM comment WHERE post_id = ? ORDER BY created_at ASC LIMIT ?,?", pId, offset, numResults)</p>
<p>This does the job, however, regarding that using offsets is discouraged for performance reasons, I'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 > ?</code>so whatever, though I'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'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've seen a lot of these generic 'go figure out how to do it yourself' in go community. I think it's simple arrogance and discouraging newbies, not help. </p></pre>circuitously: <pre><p>They're not saying you have to work it out yourself, they're saying you should build something as opposed to using a pagination package. You'll still get suggestions on how to approach it.</p></pre>xiamk: <pre><p>Hey @gar44, I know you're using sqlx but maybe you could consider upper-db for your next project, this is how'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传