如何将Go与MySQL连接以保存数据并再次渲染? [关闭]

I need to know how to connect my template-works with Go with MySQL phpMyAdmin to save data in tables and render it somewhere ?

Can any one help me ?

To work with mysql you have to connect database. So you need some mysql package of golang:

import "database/sql"
import _ "github.com/go-sql-driver/mysql"

and then connect database using sql.Open() like below:

db, err := sql.Open("mysql", "user:password@/dbname")

Now you have handler like register user, first you have to open a template for insert user details and then post those details to handler then get those inputed data on form, then insert with mysql instance db.

Below code shows, how to render template from go handler:

var register = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
        t, _ := template.ParseFiles("register.html")
        t.Execute(w, p)
    } else {
        //get posted data here and insert into mysql.
    }

})

For more details please follow the link