<p>For reference, my current project is here: <a href="https://github.com/Jake-Ruston/a-level-cw/tree/master/src" rel="nofollow">https://github.com/Jake-Ruston/a-level-cw/tree/master/src</a></p>
<p>So in <code>model/User.go</code> I have my <code>Register</code> function, which is supposed to validate inputs & then insert that data into the database.</p>
<p>In <code>handler/register.go</code> I have the lines</p>
<pre><code>if err := user.Register(uType, cpWord); err != nil {
fmt.Println("ERROR Register:", err)
return
}
</code></pre>
<p>which just calls that function. </p>
<p>I don't know how to properly make use of connecting to MySQL and executing a query. Lines <code>22>24</code> in <code>User.go</code> is where I would put the query, which I assumed would be something along the lines of</p>
<pre><code>if _, e := db.Exec("INSERT INTO login SET user_type='?', username='?', password='?'", uType, u.Username, hashedPassword); e != nil {
fmt.Println("ERROR Exec:", err)
return e
}
</code></pre>
<p>but where do I put the <code>sql.Open("mysql", "root:redacted@/exam-management")</code> line? I'm going to need to query the database in multiple files so it doesn't make sense to keep opening it everywhere.</p>
<hr/>**评论:**<br/><br/>nstratos: <pre><p>Please have a look at this thread. I think the answer will help you: </p>
<p><a href="https://forum.golangbridge.org/t/best-practice-to-use-go-sql-driver-mysql-package/8028" rel="nofollow">https://forum.golangbridge.org/t/best-practice-to-use-go-sql-driver-mysql-package/8028</a></p></pre>irene634: <pre><p>I would recommend using this:
<a href="https://github.com/jinzhu/gorm" rel="nofollow">https://github.com/jinzhu/gorm</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传