package main import "github.com/mxk/go-sqlite/sqlite3" type User struct { id int64 name string isAdmin bool } func createUser(db *sqlite3.Conn, name string, isAdmin bool) (User, error) { user := getUserForName(db, name) //Check if a user with this name already exists if (user.id == 0) { args := sqlite3.NamedArgs{"$a": name, "$b": isAdmin} sql := "INSERT INTO users (name, admin) VALUES ($a, $b)" err := db.Exec(sql, args) return User{id:db.LastInsertId(), name:name, isAdmin:isAdmin}, err } else { return user, nil } } func getUserForId(db *sqlite3.Conn, id int) User { args := sqlite3.NamedArgs{"$a": id} sql := "SELECT * FROM users WHERE id = $a" s, err := db.Query(sql, args) row := make(sqlite3.RowMap) for ; err == nil ; err = s.Next() { var rowid int64 s.Scan(&rowid, row) // Assigns 1st column to rowid, the rest to row user := User{id:rowid, name:row["name"].(string), isAdmin:row["admin"].(bool)} return user } //If we get here there are no matching users return User{id:0, name:"", isAdmin:false} } func getUserForName(db *sqlite3.Conn, name string) User { args := sqlite3.NamedArgs{"$a": name} sql := "SELECT * FROM users WHERE name = $a" s, err := db.Query(sql, args) row := make(sqlite3.RowMap) for ; err == nil ; err = s.Next() { var rowid int64 s.Scan(&rowid, row) user := User{id:rowid, name:row["name"].(string), isAdmin:row["admin"].(bool)} return user } //If we get here there are no matching users return User{id:0, name:"", isAdmin:false} }