I'm using the code in func Root
as a guide to create another method Login
shown below. In particular, in Root
, I assign the literal Book{}
to b
and then use the result in the Scan
. That code doesn't throw any errors (although I'm not sure if it's nice code), but when I try to do something similar in the Login
function, which I'm modifying from this blogpost, I get this error
cannot use User literal (type User) as type *User in assignment
for what it's worth, I also get this error right above when I compile
no new variables on left side of :=
but aren't I doing the same thing in the second method, namely assigning the literal u := User{}
to a variable and then using it in the scan?
Can you explain using the code below when you can and can't use a literal for type in an assignment?
- func Root(w http.ResponseWriter, r *http.Request) {
-
- rows, err := db.Query("SELECT title, author, description FROM books")
- books := []Book{}
- for rows.Next() {
- b := Book{}
- err := rows.Scan(&b.Title, &b.Author, &b.Description)
- PanicIf(err)
- books = append(books, b)
- }
- ...//code ommitted
-
-
-
- func Login(password, email string) (u *User, err error) {
- u := User{}
- db.QueryRow("select * from users where email=$1 ", email).Scan(&u.Id, &u.Password, &u.Email)
- if err != nil {
- return
- }
-
- err = bcrypt.CompareHashAndPassword(u.Password, []byte(password))
- if err != nil {
- u = nil
- }
- return
- }