转到:未使用的变量 [英] Go: Unused variable

查看:36
本文介绍了转到:未使用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在下面的代码中执行一条SQL语句.但是, sqlRes 未使用,因此无法编译.我不需要该变量,但是我需要声明它,因为 Exec()返回多个值.

I am trying to execute a SQL statement in the code below. However, sqlRes is unused and therefore cannot be compiled. I do not need the variable, but I need to declare it because Exec() returns multiple values.

我该如何处理?

stmt, err := db.Prepare("INSERT person SET name=?")
sqlRes, err := stmt.Exec(person.Name)

推荐答案

规范:

Replace sqlRes with the blank identifier (_). From the spec:

空白标识符提供了一种忽略分配中右侧值的方法:

The blank identifier provides a way to ignore right-hand side values in an assignment:

_ = x       // evaluate x but ignore it
x, _ = f()  // evaluate f() but ignore second result value

示例:

stmt, err := db.Prepare("INSERT person SET name=?")
_, err = stmt.Exec(person.Name)

这篇关于转到:未使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆