Answer the question
In order to leave comments, you need to log in
How to get value from db?
I am using https://github.com/jinzhu/gorm
Here is the code
func templateAuth(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
row := db.Table("use_api").Where("api_key = ?", vars["keyApi"]).Select("salt").Row()
h := md5.New()
io.WriteString(h, vars["keyApi"])
io.WriteString(h, row.Scan(&salt))
if h.Sum(nil) == vars["hash"] {
p := map[string]string{
"title": "Авторизация",
"domen": "localhost",
"keyApi": vars["keyApi"],
"hash": vars["hash"],
}
TemplateHtml("login.html", p, w)
} else {
p := map[string]string{
"err": "Неверный хеш",
"domen": "localhost",
}
TemplateHtml("err.html", p, w)
}
}
# command-line-arguments
./main.go:33: undefined: salt
./main.go:33: cannot use row.Scan(&salt) (type error) as type string in argument to io.WriteString
./main.go:35: invalid operation: h.Sum(nil) == vars["hash"] (mismatched types []byte and string)
Answer the question
In order to leave comments, you need to log in
io.WriteString(h, row.Scan(&salt))
Your salt variable is not defined, but the compiler writes everything clearly.
if h.Sum(nil) == vars["hash"]
And here you are comparing different types, []byte and string. Convert to one type.
UPD.
like this string(h.Sum(nil)) == string(vars["hash"])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question