go/main.go
author viric <viriketo@gmail.com>
Mon, 03 Sep 2012 23:35:31 +0200
branchgo
changeset 7 d5750f4e6e8c
permissions -rw-r--r--
Adding a first attempt at go code
package main

import (
	"database/sql"
	"github.com/coopernurse/gorp"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"os"
)

type Sentence struct {
	Id       int64
	Word     string
	Sentence string
}

func main() {
	db, err := sql.Open("sqlite3", "lingvigilo.db")
	if err != nil {
		log.Fatal("Fatal error: ", err)
	}
	defer db.Close()

	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}

	dbmap.TraceOn("dbmap", log.New(os.Stderr, "sql", 0))

	dbmap.AddTableWithName(Sentence{}, "sentences").SetKeys(true, "Id")

	dbmap.DropTables()

	err = dbmap.CreateTables()
	if err != nil {
		log.Fatal("Fatal error: ", err)
	}

	mySentence := &Sentence{0, "potato", "Open the potato"}

	err = dbmap.Insert(mySentence)
	if err != nil {
		log.Fatal("Fatal error: ", err)
	}
}