Re: [go-nuts] What am I missing here? FYI, I'm completely new to golang xD

2018-09-03 Thread Mhd Shulhan
On Mon, 3 Sep 2018, 13:03 'sebastian andersen' via golang-nuts, <
golang-nuts@googlegroups.com> wrote:

> This code want's me to believe that I have not declared any of the
> variables that I am currently trying to declare:
>
> func deleteRow() {
> db, err := sql.Open("mysql", "root@/testshit")
> checkErr(err)
>
> // delete
> stmt, err = db.Prepare("delete from userinfo where uid=?")
> checkErr(err)
>
> res, err = stmt.Exec(id)
> checkErr(err)
>
> affect, err = res.RowsAffected()
> checkErr(err)
>
> fmt.Println(affect)
>
> db.Close()
> }
>

You need to use short assignment ":=" for variable that is not declared
previously.

In your case, "stmt, err = ..." should be " stmt, err := ..."

Tour about variable  short assignment: https://tour.golang.org/basics/10

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] What am I missing here? FYI, I'm completely new to golang xD

2018-09-03 Thread 'sebastian andersen' via golang-nuts
This code want's me to believe that I have not declared any of the 
variables that I am currently trying to declare:

func deleteRow() {
db, err := sql.Open("mysql", "root@/testshit")
checkErr(err)

// delete
stmt, err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)

res, err = stmt.Exec(id)
checkErr(err)

affect, err = res.RowsAffected()
checkErr(err)

fmt.Println(affect)

db.Close()
}


Why is that? I don't understand that, it works fine in this context:

func main() {
db, err := sql.Open("mysql", "root@/testshit")
checkErr(err)

// insert
stmt, err := db.Prepare("INSERT userinfo SET 
username=?,departname=?,created=?")
checkErr(err)

res, err := stmt.Exec("astaxie", "dong", "2012-12-09")
checkErr(err)

id, err := res.LastInsertId()
checkErr(err)

fmt.Println(id)
// update
stmt, err = db.Prepare("update userinfo set username=? where uid=?")
checkErr(err)

res, err = stmt.Exec("astaxieupdate", id)
checkErr(err)

affect, err := res.RowsAffected()
checkErr(err)

fmt.Println(affect)

// query
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)

for rows.Next() {
var uid int
var username string
var department string
var created string
err = rows.Scan(, , , )
checkErr(err)
fmt.Println(uid)
fmt.Println(username)
fmt.Println(department)
fmt.Println(created)
}

// delete
stmt, err = db.Prepare("delete from userinfo where uid=?")
checkErr(err)

res, err = stmt.Exec(id)
checkErr(err)

affect, err = res.RowsAffected()
checkErr(err)

fmt.Println(affect)

db.Close()

}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.