package main

import (
    "database/sql"
    "log"

    _ "github.com/mattn/go-sqlite3"
)

var createUsers = `
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name VARCHAR
);
`

var insertUser = `INSERT INTO users (id, name) VALUES (?, ?)`

var selectUser = `SELECT id, name FROM users`

func main() {
    
    db, err := sql.Open("sqlite3", ":memory:")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    if _, err := db.Exec(createUsers); err != nil {
        panic(err)
    }

    if _, err := db.Exec(insertUser, 23, "skidoo"); err != nil {
        panic(err)
    }

    var id int64
    var name string
    row := db.QueryRow(selectUser)

    if err = row.Scan(&id, &name); err != nil {
        panic(err)
    }

    log.Println(id, name)





}
package main

import (
	"database/sql"
	"log"

	_ "github.com/mattn/go-sqlite3"
)

var createUsers = `
CREATE TABLE users (
    id INTEGER,
    name VARCHAR
);
`

var insertUser = `INSERT INTO users (id, name) VALUES (?, ?)`

var selectUser = `SELECT id, name FROM users`

type User struct {
	ID   int64
	Name sql.NullString
}

func (u User) String() string {
	if u.Name.Valid {
		return u.Name.String
	}
	return "No name"
}

func main() {
	// Connect to an in-memory sqlite3 instance
	db, err := sql.Open("sqlite3", ":memory:")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	// Create the table
	if _, err := db.Exec(createUsers); err != nil {
		panic(err)
	}

	// Insert a user without a name
	if _, err := db.Exec(insertUser, 1, nil); err != nil {
		panic(err)
	}

	// Select a user
	var user User
	row := db.QueryRow(selectUser)
	if err = row.Scan(&user.ID, &user.Name); err != nil {
		panic(err)
	}
	log.Println(user)
}

INSERT a NULL value and SELECT it back out
INSERT a NULL value and SELECT it back out
