dongziya9863 2016-04-05 09:08
浏览 29

转到-在查询周围使用if语句时出错[重复]

This question already has an answer here:

I am trying to run this - which would work fine in PHP but in Go I am getting an error:

if strings.EqualFold(update_status, "live") {
    stmt, err := dbmap.Prepare(
        "UPDATE Apps SET version=?,deploy_status=?,status=?,live_date=? WHERE id=?")
} else {
    stmt, err := dbmap.Prepare(
        "UPDATE Apps SET version=?,deploy_status=?,status=? WHERE id=?")
}

I get the error of controllers/apps.go:1095: undefined: stmt in stmt.Exec.

How can I change this so it will work?

Thanks

</div>
  • 写回答

1条回答 默认 最新

  • dongyong8098 2016-04-05 09:13
    关注

    Your problem is that you are using Short Variable Declaration, so your stmt and err variables are scoped to the inner block of your conditions (both the if and the else.

    This should do the trick:

    var stmt *sql.Stmt
    var err error
    if strings.EqualFold(update_status, "live") {
        stmt, err = dbmap.Prepare(
            "UPDATE Apps SET version=?,deploy_status=?,status=?,live_date=? WHERE id=?")
    } else {
        stmt, err = dbmap.Prepare(
            "UPDATE Apps SET version=?,deploy_status=?,status=? WHERE id=?")
    }
    //stmt is still defined here
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100