golang从struct返回第一个字段

I am trying to return all user information given ONE attribute which can either be user_id, email, or name.

u := User{
 Email: "goda@go.com"
 })
k := User {
Name: "john"
}

ReturnUserInfo(u)
ReturnUserInfo(k)

I invoke the function passing a User struct with only one field. Then I want to parse the field without EXPLICITLY saying the email. In the end I get the user information by passing an implicit field (user_id OR email etc.)

func ReturnUserInfo(u User) (y User){
    // Retrieve first field from u and set them to field and value.
    // NOT explicitly stating field := email, value := u.Email

    db, _ := sql.Open("mysql", "root:password@/users")
    _ := db.QueryRow("SELECT user_id, name, email FROM users WHERE ? = ?", field, value).Scan(
        &u.User_id, &u.Name, &u.Email)
    y = User{
        User_id: u.User_id,
        Name: u.Name,
        Email: u.Email,
    }
    return y

}

I am not sure if I am able to do this in Golang, but I am creating this function so I do not have to explicitly tell Go to look for a particular attribute to return a user's information.

What you want has several bad or at least controversary design decision.

1) SQL statement you cannot use "WHERE ? = ?". Name of the column cannot be replaced with ?, only values can. It is not caused by golang, but by database, because when it obtain request create a plan how to obtain a data (which index use or full table scan or ...).

That means you have to create different string of query for each column, you want use in where clause. It can look something like this:

    func GetUserInfoByColumn(field string) *User, error {
query := fmt.Sprintf("SELECT user_id, name, email FROM users WHERE %s = ?", column)
    var u User
    err := db.QueryRow(query, value).Scan(&u.User_id, &u.Name, &u.Email)
    if err != nil {return nil, err}
    return &u, nil
    }

More efective than this is using a closure and create query there.

2) db.QueryRow looks like another problem. You can use it only if you are sure, that only one row matches your criteria. If you are not so sure, please use db.Query.

3) In go you can use reflect to get a name of all items in struct. But if your struct has only few field it is too complicated. In our case you can simple create all necessary functions very simple.

4) I don't understand why you tries to fill always just one field of struct and put the filter into struct. Would not be better have more functions and just call UserInfoByName, or UserInfoByEmail at proper place of code? Why you tries to choose a filter accoring field?