stmt, err := db.Prepare("SELECT name FROM users WHERE id = ?") if err != nil { log.Fatal(err) } defer stmt.Close() var name string err = stmt.QueryRow(123).Scan(&name) if err != nil { log.Fatal(err) } fmt.Println(name)
stmt, err := db.Prepare("INSERT INTO users (name, email) VALUES (?, ?)") if err != nil { log.Fatal(err) } defer stmt.Close() result, err := stmt.Exec("Alice", "[email protected]") if err != nil { log.Fatal(err) } id, _ := result.LastInsertId() fmt.Println(id)Here, we prepare a statement with two placeholders for the `name` and `email` values. We execute the statement with specific values, and use the `LastInsertId()` method to get the ID of the new row. The `database/sql` package is part of the standard library in Go, so no external packages are required.