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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)