Why is this so? Here's my code:
import (
"database/sql"
"fmt"
"reflect"
"github.com/fatih/structs"
)
type UserLogin struct {
Username string
Password string
}
func Login() {
row := sql.QueryRow("SELECT username, password FROM users WHERE username=?", "golang")
userLoginKeys := structs.Names(UserLogin{})
keys := make([]interface{}, len(userLoginKeys), len(userLoginKeys))
for i, val := range userLoginKeys {
keys[i] = &val
fmt.Println(val)
}
fmt.Println(keys)
_ := row.Scan(keys...)
v1 := reflect.ValueOf(keys[0]).Elem().String()
v2 := reflect.ValueOf(keys[1]).Elem().String()
fmt.Println(v1)
fmt.Println(v2)
}
It prints
Username
Password
[0xc4201ca2c0 0xc4201ca2c0]
$2a$10$F6hR0scvtbFDx0l1GR.OX.ZweozUzwKVTG3H8GBQxpYCEdFifDrzy
$2a$10$F6hR0scvtbFDx0l1GR.OX.ZweozUzwKVTG3H8GBQxpYCEdFifDrzy
As you can see, keys
contains the same address to two different strings. As a result, their values are the same.
My goal is to map username
and password
to UserLogin
struct.