两个字符串具有相同的指针地址。

huangapple go评论83阅读模式
英文:

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 包含了指向两个不同字符串的相同地址。因此,它们的值是相同的。

我的目标是将 usernamepassword 映射到 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] = &amp;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&lt;len(userLoginKeys); i++ {
    keys[i] = &amp;userLoginKeys[i]
    fmt.Println(keys[i])
}

答案3

得分: 0

在你的循环中,你正在将keys[i] = &val赋值,这将为每个数组元素分配相同的地址。这也意味着你只得到了最后一个值。你可能想要的是keys[i] = val(并将映射设置为使用字符串而不是*字符串)。

英文:

You are assigning keys[i] = &amp;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).

huangapple
  • 本文由 发表于 2017年2月26日 09:33:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/42463720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定