donglin5770 2015-10-16 21:00
浏览 117
已采纳

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.

  • 写回答

1条回答 默认 最新

  • duanpai9945 2015-10-16 21:58
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?