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
viriketo@7
     1
package main
viriketo@7
     2
viriketo@7
     3
import (
viriketo@7
     4
	"database/sql"
viriketo@7
     5
	"github.com/coopernurse/gorp"
viriketo@7
     6
	_ "github.com/mattn/go-sqlite3"
viriketo@7
     7
	"log"
viriketo@7
     8
	"os"
viriketo@7
     9
)
viriketo@7
    10
viriketo@7
    11
type Sentence struct {
viriketo@7
    12
	Id       int64
viriketo@7
    13
	Word     string
viriketo@7
    14
	Sentence string
viriketo@7
    15
}
viriketo@7
    16
viriketo@7
    17
func main() {
viriketo@7
    18
	db, err := sql.Open("sqlite3", "lingvigilo.db")
viriketo@7
    19
	if err != nil {
viriketo@7
    20
		log.Fatal("Fatal error: ", err)
viriketo@7
    21
	}
viriketo@7
    22
	defer db.Close()
viriketo@7
    23
viriketo@7
    24
	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
viriketo@7
    25
viriketo@7
    26
	dbmap.TraceOn("dbmap", log.New(os.Stderr, "sql", 0))
viriketo@7
    27
viriketo@7
    28
	dbmap.AddTableWithName(Sentence{}, "sentences").SetKeys(true, "Id")
viriketo@7
    29
viriketo@7
    30
	dbmap.DropTables()
viriketo@7
    31
viriketo@7
    32
	err = dbmap.CreateTables()
viriketo@7
    33
	if err != nil {
viriketo@7
    34
		log.Fatal("Fatal error: ", err)
viriketo@7
    35
	}
viriketo@7
    36
viriketo@7
    37
	mySentence := &Sentence{0, "potato", "Open the potato"}
viriketo@7
    38
viriketo@7
    39
	err = dbmap.Insert(mySentence)
viriketo@7
    40
	if err != nil {
viriketo@7
    41
		log.Fatal("Fatal error: ", err)
viriketo@7
    42
	}
viriketo@7
    43
}