package main
import (
"encoding/json"
"gorilla/mux"
"log"
"net/http"
"database/sql"
_ "go-sql-driver/mysql"
)
func GetPeople(w http.ResponseWriter, r *http.Request, db *sql.DB) {
json.NewEncoder(w).Encode(db.QueryRow("SELECT * from test.animal"))
}
func main() {
db, err := sql.Open("mysql", "root:mariadb@tcp(127.0.0.1:3306)/test")
if err != nil {
panic(err.Error())
}
defer db.Close()
router := mux.NewRouter()
router.HandleFunc("/people", GetPeople).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", router))
}
I need to make my db recognized inside the GetPeople function but giving the function a third parameter (db *sql.DB) is impossible.
How can I solve it? Thank you