New to Go, need help with my first attempt.

agolangf · · 603 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello,</p> <p>i&#39;m fairly new to Go and i wanna mainly use to for web-development. Coming from PHP and being used to frameworks doing everything for me, this seems to be quite a challenge :P. I decided to start building tools that i&#39;d need in order to not go mental, one of those tools would be a small &#34;ORM&#34; that helps keeping my models (structs) in sync with the database, in this case Postgres. </p> <p>I want to build a CLI tool that helps generating getters/setters and update the database schema. Reflections seem to be the right way to do so, but i have 1 problem: how do i use a struct inside my compiled CLI-tool? F.e. i need it to do following: </p> <pre><code>$mytool generate mystruct -&gt; generate getters/setters for file &#34;mystruct.go&#34; </code></pre> <p>and </p> <pre><code>$mytool schema:update -&gt; look at all struct files and update DB for changes </code></pre> <p>My problem is: how do i use a struct based on an argument? I can&#39;t import it, because the tool is already compiled, right? What am i missing?</p> <p>Regards</p> <hr/>**评论:**<br/><br/>ElectricSpock: <pre><p>This doesn&#39;t sound like a reflection problem, rather parsing the code. I don&#39;t want to do the full research for you, but I think you just want to generate AST from the file. Why not starting your research with this package: <a href="https://golang.org/pkg/go/ast/" rel="nofollow">https://golang.org/pkg/go/ast/</a>.</p> <p>EDIT: Actually, there&#39;s parsing package as well: <a href="https://golang.org/pkg/go/parser/" rel="nofollow">https://golang.org/pkg/go/parser/</a>.</p></pre>ChristophBerger: <pre><p>There are a couple of ORM libraries available on GitHub (like gorm, gorp, or xorm, just to name a few popular ones). Would one of these fit your purposes? (Disclaimer: I don&#39;t know any of these in detail.) </p> <p>Or if you want to explicitly write your own ORM tool, these libraries would show how others have approached the same problem. </p></pre>beertocode: <pre><p>Agree with ChristophBerger. Maybe have a look at ORM tools first. E.g. have a look at gorm: <a href="https://github.com/jinzhu/gorm" rel="nofollow">https://github.com/jinzhu/gorm</a></p> <p>You can just define the struct and let gorm create the DB for you, then you can retrieve objects and map them to the struct or save a struct straight to the database without writing any getters or setters. (to be honest it sounds odd a bit to me if you want to have code generated for every single get/set. So, I might misunderstood you). Another cool thing is automigration feature. When you e.g. add a new field into a struct, gorm can automatically create that new column for you.</p> <p>However, so far I feel that the go community tend to rather build the things straight from the standard library. So you should always consider whether it&#39;s faster to develop now and easier to maintain later if you just write a few functions by yourself rather than relying on an ORM.</p> <p>Have fun with go!</p></pre>

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

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