<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>
