boltease/example/main.go

77 lines
1.5 KiB
Go

package main
import (
"fmt"
"os"
"git.bullercodeworks.com/brian/boltease"
)
type ExampleType struct {
Name string `boltease:"name"`
Age int `boltease:"age"`
}
func (e ExampleType) String() string {
return fmt.Sprintf("{\"Name\":\"%s\",\"Age\":%d}", e.Name, e.Age)
}
type ExampleSubType struct {
SubName string `boltease:"subname"`
}
func main() {
db, err := boltease.Create("example.db", 0600, nil)
if err != nil {
fmt.Printf("Error Opening File: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("# Example 1: Simple")
var v string
err = db.GetForInterface([]string{"examples", "example1"}, "name", &v)
if err != nil {
fmt.Println("Error:", err.Error())
}
fmt.Println("Name:", v)
var age int
err = db.GetForInterface([]string{"examples", "example1"}, "age", &age)
if err != nil {
fmt.Println("Error:", err.Error())
}
fmt.Println("Age:", age)
fmt.Println("")
fmt.Println("# Example 2: LoadStruct, simple")
var name string
err = db.LoadStruct(
[]string{"examples", "example1", "name"},
&name,
)
fmt.Println("Name:", name)
if err != nil {
fmt.Println("Err:", err)
}
fmt.Println("")
fmt.Println("# Example 3: Struct")
err = db.SaveStruct(
[]string{"examples", "example1"},
ExampleType{
Name: "Example 1",
Age: 5,
})
if err != nil {
fmt.Printf("Error saving struct: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("Loading into Struct")
newStruct := ExampleType{}
err = db.LoadStruct(
[]string{"examples", "example1"},
&newStruct,
)
fmt.Println(newStruct)
}