V
V
v- death2015-08-24 02:49:19
MySQL
v- death, 2015-08-24 02:49:19

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

Mistakes
# 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)

As I understand it, I didn’t receive the cell data, but I got xs what. Thanks in advance.
Here is the full script with normal highlighting

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-08-24
@FireGM

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"])

_
_ _, 2015-08-24
@AMar4enko

var salt string
row.Scan(&salt) ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question