英文:
Two strings the have same pointer address
问题
为什么会这样呢?这是我的代码:
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)
}
它输出:
Username
Password
[0xc4201ca2c0 0xc4201ca2c0]
$2a$10$F6hR0scvtbFDx0l1GR.OX.ZweozUzwKVTG3H8GBQxpYCEdFifDrzy
$2a$10$F6hR0scvtbFDx0l1GR.OX.ZweozUzwKVTG3H8GBQxpYCEdFifDrzy
正如你所看到的,keys
包含了指向两个不同字符串的相同地址。因此,它们的值是相同的。
我的目标是将 username
和 password
映射到 UserLogin
结构体中。
英文:
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.
答案1
得分: 2
将keys[i] = &val
更改为keys[i] = &userLoginKeys[i]
,这样你就可以获得不同的地址。
英文:
Change keys[i] = &val
to keys[i] = &userLoginKeys[i]
, so you can get different addresses.
答案2
得分: 1
for i, val := range userLoginKeys {
keys[i] = &val
fmt.Println(val)
}
在这里,val是一个独立的变量,你正在将其地址分配给keys[i]。所以每次你都得到相同的地址。
尝试这样写:
for i:=0; i<len(userLoginKeys); i++ {
keys[i] = &userLoginKeys[i]
fmt.Println(keys[i])
}
英文:
for i, val := range userLoginKeys {
keys[i] = &val
fmt.Println(val)
}
here, val is an independent variable whose address you are assigning to keys[i]. So both time you are getting same address.
Try this:
for i:=0; i<len(userLoginKeys); i++ {
keys[i] = &userLoginKeys[i]
fmt.Println(keys[i])
}
答案3
得分: 0
在你的循环中,你正在将keys[i] = &val
赋值,这将为每个数组元素分配相同的地址。这也意味着你只得到了最后一个值。你可能想要的是keys[i] = val
(并将映射设置为使用字符串而不是*字符串)。
英文:
You are assigning keys[i] = &val
in your loop, this is assigning the same address to each array element. This also means that you are only getting the last value. What you probably want is keys[i] = val
(and set the map to use strings instead of *strings).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论