First let me say that I am a beginner (started a few days ago) with golang and I am in the process of learning how to practice the language. My goal is to create a Rest API web interface that queries the database and returns data to the user.
I was able to successfully create a simple API using martini ( https://github.com/go-martini/martini ) and connect to the MySQL database using https://github.com/go-sql-driver/mysql . Right now, my problem is how to pass the param variable from the API request to my request. Here is my current code:
package main
import (
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql",
"root:root@tcp(localhost:8889)/test")
m := martini.Classic()
var str string
m.Get("/hello/:user", func(params martini.Params) string {
var userId = params["user"]
err = db.QueryRow(
"select name from users where id = userId").Scan(&str)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
return "Hello " + str
})
m.Run()
defer db.Close()
}
, - () userId. , . , .
- ? , , Go!