<p>Hi,</p>
<p>I've just started a project where I write a lot of SQL queries. Currently I write them as a multiline string variable. Is there a way to store them in proper SQL files (so I can enjoy color syntax and other good things :) ) and generate these string variables at the build time ? Thank you in advance</p>
<p>Edit : I don't want to use an ORM as I use advanced PostGis queries and want to limit dependencies.</p>
<p><strong>EDIT2 :</strong> Thank you all for the answer, go generate seems the way to go (ah ah), I'll try it !</p>
<hr/>**评论:**<br/><br/>dinkumator: <pre><p>You could try <code>go generate</code> with something like this:
<a href="https://github.com/jteeuwen/go-bindata" rel="nofollow">https://github.com/jteeuwen/go-bindata</a>
(bonus is that debug mode accesses files directly)</p></pre>bietekwiet: <pre><p>i use this often and it works flawlessly.</p></pre>TheMerovius: <pre><p>Not at build time, go intentionally doesn't support build-time processing. But <a href="https://blog.golang.org/generate" rel="nofollow">go generate</a> is usually used for this kind of stuff. Just write it into an sql file and then write a short go program (hedged by a <code>build +ignore</code> or something) that generates a .go file from then. Then add a <code>go:generate</code> comment somewhere that <code>go run</code>s that program and run <code>go generate</code> before/at each commit.</p>
<p>I don't know if there are ready-made solutions to this. Another way would be to use a pre-existing solution for statically compile in files from source and then write code to read from disk/that statically compiled in FS at runtime to find the queries.</p></pre>bear1728: <pre><p>You could use go generate to embed the contents of some sql files into your program before compiling. There was post about them here:</p>
<p><a href="https://mattjibson.com/blog/2014/11/19/esc-embedding-static-assets/" rel="nofollow">https://mattjibson.com/blog/2014/11/19/esc-embedding-static-assets/</a></p>
<p>I also wrote a <em>very</em> simple version here:</p>
<p><a href="https://github.com/tscholl2/embd" rel="nofollow">https://github.com/tscholl2/embd</a></p>
<p>Most of these won't deal with multiple queries in the same file, but you may be able to figure something our for that.</p></pre>flexd: <pre><p><a href="https://github.com/nleof/goyesql" rel="nofollow">https://github.com/nleof/goyesql</a></p>
<p><code>nleof/goyesql</code> does exactly what you want!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传