在Golang中将字符串传递给预备语句

The idea is I want to update the status and returning the id only if the status is different.

So, I have a prepared statement like this:

var theQuery = `UPDATE process SET status=$1 WHERE status!=$1 AND id=$2 RETURNING id`

Then, I called it with this:

err = statement.QueryRow("set", 12).Scan(&id)

Then there is an error appear like this.

runtime error: invalid memory address or nil pointer dereference

When I tried:

var theQuery = `UPDATE process SET status='$1' WHERE status!='$1' AND id=$2 RETURNING id`

It runs. Then, when I run it again, I am expecting to get no rows, but it still returning the id. It looked like it still updating the rows and ignored the status!='$1' part.

Thank you

So, I just find the solution. Instead of using $1 twice, the prepared statement will received 3 arguments:

var theQuery = `UPDATE process SET status=$1 WHERE status!=$2 AND id=$3 RETURNING id`

Then, I call the prepared statement like this.

status := "set"
err = statement.QueryRow(status, status, 12).Scan(&id)

I know maybe this is not the best approach to solve the problem. But it worked for me.