├── .gitignore ├── .travis.yml ├── README.md ├── example ├── postgres │ └── example.go └── sqlite3 │ └── example.go ├── sqlkv.go └── sqlkv_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | example/sqlite3/example.db 2 | test.db 3 | test.out 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: 4 | - 1.2 5 | - tip 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## SQL-based key/value store for Golang [![Build Status](https://travis-ci.org/laurent22/go-sqlkv.png)](https://travis-ci.org/laurent22/go-sqlkv) 2 | 3 | SqlKv provides an SQL-based key/value store for Golang. It can work with any of the database types supported by the built-in `database/sql` package. 4 | 5 | It can be used, for example, to easily store configuration values in Sqlite, or to build a simple caching system when something more advanced like memcached or Redis is not available. 6 | 7 | ## Installation 8 | 9 | go get github.com/laurent22/go-sqlkv 10 | 11 | ## Usage 12 | 13 | The first step is to initialize a new database connection. The package expects the connection to remain open while being used. For example, using Sqlite: 14 | 15 | ```go 16 | db, err := sql.Open("sqlite3", "example.db") 17 | if err != nil { 18 | panic(err) 19 | } 20 | defer db.Close() 21 | ``` 22 | 23 | Then create a new SqlKv object and pass it the db connection and the table name. The table will automatically be created if it does not already exist. 24 | 25 | ```go 26 | store := sqlkv.New(db, "kvstore") 27 | ``` 28 | 29 | The value can then be retrived and set using the provided methods: 30 | 31 | - `String(name)` / `SetString(name, value)` 32 | - `Int(name)` / `SetInt(name, value)` 33 | - `Float(name)` / `SetFloat(name, value)` 34 | - `Bool(name)` / `SetBool(name, value)` 35 | - `Time(name)` / `SetTime(name, value)` 36 | 37 | When getting a value, a default value can be provided using the alternative `D()` methods: 38 | 39 | - `StringD(name, "some default")` 40 | - `IntD(name, 12345)` 41 | - `FloatD(name, 3.14)` 42 | - `BoolD(name, true)` 43 | - `TimeD(name, time.Now())` 44 | 45 | In order to keep the API simple, all the errors are handled internally when possible. If an error cannot be handled (eg. cannot read or write to the database), the method will panic. 46 | 47 | If a key is missing, each Get method will return Golang's default zero value for this type. The zero values are: 48 | 49 | - String: "" 50 | - Int: 0 51 | - Float: 0 52 | - Bool: false 53 | - Time: time.Time{} (Test with `time.IsZero()`) 54 | 55 | You can use `HasKey` to check if a key really exists. The method `Del` is also available to delete a key. 56 | 57 | ## Postgres support 58 | 59 | To use the library with Postgres, you need to specify the driver name just after having created the store object: 60 | 61 | ```go 62 | store := sqlkv.New(db, "kvstore") 63 | store.SetDriverName("postgres") 64 | ``` 65 | 66 | A more detailed example is in [example/postgres/example.go](example/postgres/example.go) 67 | 68 | ## API reference 69 | 70 | http://godoc.org/github.com/laurent22/go-sqlkv 71 | 72 | ## Full example 73 | 74 | ```go 75 | package main 76 | 77 | import ( 78 | "database/sql" 79 | "fmt" 80 | "os" 81 | "time" 82 | 83 | _ "github.com/laurent22/go-sqlkv" 84 | _ "github.com/mattn/go-sqlite3" 85 | ) 86 | 87 | func main() { 88 | os.Remove("example.db") 89 | db, err := sql.Open("sqlite3", "example.db") 90 | if err != nil { 91 | panic(err) 92 | } 93 | defer db.Close() 94 | 95 | store := sqlkv.New(db, "kvstore") 96 | 97 | store.SetString("username", "John") 98 | fmt.Println(store.String("username")) 99 | 100 | store.SetInt("age", 25) 101 | fmt.Println(store.Int("age")) 102 | 103 | store.SetFloat("pi", 3.14) 104 | fmt.Println(store.Float("pi")) 105 | 106 | store.SetTime("today", time.Now()) 107 | fmt.Println(store.Time("today")) 108 | 109 | store.SetBool("enabled", true) 110 | fmt.Println(store.Bool("enabled")) 111 | 112 | fmt.Println(store.HasKey("username")) 113 | 114 | store.Del("username") 115 | fmt.Println(store.String("username")) 116 | 117 | fmt.Println(store.HasKey("username")) 118 | } 119 | ``` 120 | 121 | ## License 122 | 123 | The MIT License (MIT) 124 | 125 | Copyright (c) 2013-2014 Laurent Cozic 126 | 127 | Permission is hereby granted, free of charge, to any person obtaining a copy 128 | of this software and associated documentation files (the "Software"), to deal 129 | in the Software without restriction, including without limitation the rights 130 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 131 | copies of the Software, and to permit persons to whom the Software is 132 | furnished to do so, subject to the following conditions: 133 | 134 | The above copyright notice and this permission notice shall be included in 135 | all copies or substantial portions of the Software. 136 | 137 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 138 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 139 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 140 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 141 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 142 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 143 | THE SOFTWARE. 144 | -------------------------------------------------------------------------------- /example/postgres/example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "os" 7 | "time" 8 | 9 | sqlkv "github.com/laurent22/go-sqlkv" 10 | _ "github.com/lib/pq" 11 | ) 12 | 13 | func main() { 14 | os.Remove("example.db") 15 | db, err := sql.Open("postgres", "user=postgres dbname=mydb host=localhost password=postgres") 16 | if err != nil { 17 | panic(err) 18 | } 19 | defer db.Close() 20 | 21 | store := sqlkv.New(db, "kvstore") 22 | store.SetDriverName("postgres") 23 | 24 | store.SetString("username", "John") 25 | fmt.Println(store.String("username")) 26 | 27 | store.SetInt("age", 25) 28 | fmt.Println(store.Int("age")) 29 | 30 | store.SetFloat("pi", 3.14) 31 | fmt.Println(store.Float("pi")) 32 | 33 | store.SetTime("today", time.Now()) 34 | fmt.Println(store.Time("today")) 35 | 36 | store.SetBool("enabled", true) 37 | fmt.Println(store.Bool("enabled")) 38 | 39 | fmt.Println(store.HasKey("username")) 40 | 41 | store.Del("username") 42 | fmt.Println(store.String("username")) 43 | 44 | fmt.Println(store.HasKey("username")) 45 | } 46 | -------------------------------------------------------------------------------- /example/sqlite3/example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "os" 7 | "time" 8 | 9 | sqlkv "github.com/laurent22/go-sqlkv" 10 | _ "github.com/mattn/go-sqlite3" 11 | ) 12 | 13 | func main() { 14 | os.Remove("example.db") 15 | db, err := sql.Open("sqlite3", "example.db") 16 | if err != nil { 17 | panic(err) 18 | } 19 | defer db.Close() 20 | 21 | store := sqlkv.New(db, "kvstore") 22 | 23 | store.SetString("username", "John") 24 | fmt.Println(store.String("username")) 25 | 26 | store.SetInt("age", 25) 27 | fmt.Println(store.Int("age")) 28 | 29 | store.SetFloat("pi", 3.14) 30 | fmt.Println(store.Float("pi")) 31 | 32 | store.SetTime("today", time.Now()) 33 | fmt.Println(store.Time("today")) 34 | 35 | store.SetBool("enabled", true) 36 | fmt.Println(store.Bool("enabled")) 37 | 38 | fmt.Println(store.HasKey("username")) 39 | 40 | store.Del("username") 41 | fmt.Println(store.String("username")) 42 | 43 | fmt.Println(store.HasKey("username")) 44 | } 45 | -------------------------------------------------------------------------------- /sqlkv.go: -------------------------------------------------------------------------------- 1 | package sqlkv 2 | 3 | import ( 4 | "database/sql" 5 | "strconv" 6 | "strings" 7 | "time" 8 | ) 9 | 10 | const ( 11 | PLACEHOLDER_QUESTION_MARK = 1 12 | PLACEHOLDER_DOLLAR = 2 13 | ) 14 | 15 | type SqlKv struct { 16 | db *sql.DB 17 | tableName string 18 | driverName string 19 | placeholderType int 20 | } 21 | 22 | type SqlKvRow struct { 23 | Name string 24 | Value string 25 | } 26 | 27 | func New(db *sql.DB, tableName string) *SqlKv { 28 | output := new(SqlKv) 29 | output.db = db 30 | output.tableName = tableName 31 | output.placeholderType = PLACEHOLDER_QUESTION_MARK 32 | 33 | err := output.createTable() 34 | if err != nil { 35 | panic(err) 36 | } 37 | 38 | return output 39 | } 40 | 41 | func (this *SqlKv) SetDriverName(n string) { 42 | this.driverName = n 43 | if this.driverName == "postgres" { 44 | this.placeholderType = PLACEHOLDER_DOLLAR 45 | } else { 46 | this.placeholderType = PLACEHOLDER_QUESTION_MARK 47 | } 48 | } 49 | 50 | func (this *SqlKv) createTable() error { 51 | _, err := this.db.Exec("CREATE TABLE IF NOT EXISTS " + this.tableName + " (name TEXT NOT NULL PRIMARY KEY, value TEXT)") 52 | if err != nil { 53 | return err 54 | } 55 | 56 | // Ignore error here since there will be one if the index already exists 57 | this.db.Exec("CREATE INDEX name_index ON " + this.tableName + " (name)") 58 | return nil 59 | } 60 | 61 | func (this *SqlKv) placeholder(index int) string { 62 | if this.placeholderType == PLACEHOLDER_QUESTION_MARK { 63 | return "?" 64 | } else { 65 | return "$" + strconv.Itoa(index) 66 | } 67 | } 68 | 69 | func (this *SqlKv) rowByName(name string) (*SqlKvRow, error) { 70 | row := new(SqlKvRow) 71 | query := "SELECT name, value FROM " + this.tableName + " WHERE name = " + this.placeholder(1) 72 | err := this.db.QueryRow(query, name).Scan(&row.Name, &row.Value) 73 | 74 | if err != nil { 75 | if err == sql.ErrNoRows { 76 | return nil, nil 77 | } else { 78 | return nil, err 79 | } 80 | } 81 | 82 | return row, nil 83 | } 84 | 85 | func (this *SqlKv) All() []SqlKvRow { 86 | rows, err := this.db.Query("SELECT name, `value` FROM " + this.tableName) 87 | if err != nil { 88 | if err == sql.ErrNoRows { 89 | return []SqlKvRow{} 90 | } else { 91 | panic(err) 92 | } 93 | } 94 | 95 | var output []SqlKvRow 96 | for rows.Next() { 97 | var kvRow SqlKvRow 98 | rows.Scan(&kvRow.Name, &kvRow.Value) 99 | output = append(output, kvRow) 100 | } 101 | 102 | return output 103 | } 104 | 105 | func (this *SqlKv) String(name string) string { 106 | row, err := this.rowByName(name) 107 | if err == nil && row == nil { 108 | return "" 109 | } 110 | if err != nil { 111 | panic(err) 112 | } 113 | return row.Value 114 | } 115 | 116 | func (this *SqlKv) StringD(name string, defaultValue string) string { 117 | if !this.HasKey(name) { 118 | return defaultValue 119 | } 120 | return this.String(name) 121 | } 122 | 123 | func (this *SqlKv) SetString(name string, value string) { 124 | row, err := this.rowByName(name) 125 | var query string 126 | 127 | if row == nil && err == nil { 128 | query = "INSERT INTO " + this.tableName + " (value, name) VALUES(" + this.placeholder(1) + ", " + this.placeholder(2) + ")" 129 | } else { 130 | query = "UPDATE " + this.tableName + " SET value = " + this.placeholder(1) + " WHERE name = " + this.placeholder(2) 131 | } 132 | 133 | _, err = this.db.Exec(query, value, name) 134 | if err != nil { 135 | panic(err) 136 | } 137 | } 138 | 139 | func (this *SqlKv) Int(name string) int { 140 | s := this.String(name) 141 | if s == "" { 142 | return 0 143 | } 144 | 145 | i, err := strconv.Atoi(s) 146 | if err != nil { 147 | panic(err) 148 | } 149 | 150 | return i 151 | } 152 | 153 | func (this *SqlKv) IntD(name string, defaultValue int) int { 154 | if !this.HasKey(name) { 155 | return defaultValue 156 | } 157 | return this.Int(name) 158 | } 159 | 160 | func (this *SqlKv) SetInt(name string, value int) { 161 | s := strconv.Itoa(value) 162 | this.SetString(name, s) 163 | } 164 | 165 | func (this *SqlKv) Float(name string) float32 { 166 | s := this.String(name) 167 | if s == "" { 168 | return 0 169 | } 170 | 171 | o, err := strconv.ParseFloat(s, 32) 172 | if err != nil { 173 | panic(err) 174 | } 175 | return float32(o) 176 | } 177 | 178 | func (this *SqlKv) FloatD(name string, defaultValue float32) float32 { 179 | if !this.HasKey(name) { 180 | return defaultValue 181 | } 182 | return this.Float(name) 183 | } 184 | 185 | func (this *SqlKv) SetFloat(name string, value float32) { 186 | s := strconv.FormatFloat(float64(value), 'g', -1, 32) 187 | this.SetString(name, s) 188 | } 189 | 190 | func (this *SqlKv) Bool(name string) bool { 191 | s := this.String(name) 192 | return s == "1" || strings.ToLower(s) == "true" 193 | } 194 | 195 | func (this *SqlKv) BoolD(name string, defaultValue bool) bool { 196 | if !this.HasKey(name) { 197 | return defaultValue 198 | } 199 | return this.Bool(name) 200 | } 201 | 202 | func (this *SqlKv) SetBool(name string, value bool) { 203 | var s string 204 | if value { 205 | s = "1" 206 | } else { 207 | s = "0" 208 | } 209 | this.SetString(name, s) 210 | } 211 | 212 | func (this *SqlKv) Time(name string) time.Time { 213 | s := this.String(name) 214 | if s == "" { 215 | return time.Time{} 216 | } 217 | 218 | t, err := time.Parse(time.RFC3339Nano, s) 219 | if err != nil { 220 | panic(err) 221 | } 222 | 223 | return t 224 | } 225 | 226 | func (this *SqlKv) TimeD(name string, defaultValue time.Time) time.Time { 227 | if !this.HasKey(name) { 228 | return defaultValue 229 | } 230 | return this.Time(name) 231 | } 232 | 233 | func (this *SqlKv) SetTime(name string, value time.Time) { 234 | this.SetString(name, value.Format(time.RFC3339Nano)) 235 | } 236 | 237 | func (this *SqlKv) Del(name string) { 238 | query := "DELETE FROM " + this.tableName + " WHERE name = " + this.placeholder(1) 239 | _, err := this.db.Exec(query, name) 240 | 241 | if err != nil { 242 | panic(err) 243 | } 244 | } 245 | 246 | func (this *SqlKv) Clear() { 247 | query := "DELETE FROM " + this.tableName 248 | _, err := this.db.Exec(query) 249 | 250 | if err != nil { 251 | panic(err) 252 | } 253 | } 254 | 255 | func (this *SqlKv) HasKey(name string) bool { 256 | row, err := this.rowByName(name) 257 | if row == nil && err == nil { 258 | return false 259 | } 260 | if err != nil { 261 | panic(err) 262 | } 263 | return true 264 | } 265 | -------------------------------------------------------------------------------- /sqlkv_test.go: -------------------------------------------------------------------------------- 1 | package sqlkv 2 | 3 | import ( 4 | "database/sql" 5 | "os" 6 | "testing" 7 | "time" 8 | 9 | _ "github.com/mattn/go-sqlite3" 10 | ) 11 | 12 | func getStore() *SqlKv { 13 | var err error 14 | var db *sql.DB 15 | 16 | os.Mkdir("test", 0777) 17 | 18 | os.Remove("test/database.db") 19 | db, err = sql.Open("sqlite3", "test/database.db") 20 | if err != nil { 21 | panic(err) 22 | } 23 | 24 | return New(db, "kvstore") 25 | } 26 | 27 | func clearStore(store *SqlKv) { 28 | store.db.Close() 29 | os.RemoveAll("test") 30 | } 31 | 32 | func panicHandler(t *testing.T, message string) { 33 | if r := recover(); r == nil { 34 | t.Errorf("%s: Expected call to panic, but it didn't", message) 35 | } 36 | } 37 | 38 | func noPanicHandler(t *testing.T, message string) { 39 | if r := recover(); r != nil { 40 | t.Errorf("%s: Expected call to not panic, but it did", message) 41 | } 42 | } 43 | 44 | func Test_rowByName(t *testing.T) { 45 | store := getStore() 46 | defer clearStore(store) 47 | 48 | row, err := store.rowByName("test") 49 | if err != nil { 50 | t.Errorf("Expected no error but got %s", err) 51 | } 52 | if row != nil { 53 | t.Error("Expected no data but got", row) 54 | } 55 | 56 | store.SetString("name", "lau") 57 | row, err = store.rowByName("name") 58 | if err != nil { 59 | t.Errorf("Expected no error but got %s", err) 60 | } 61 | if row == nil { 62 | t.Error("Expected data but got nil") 63 | } 64 | } 65 | 66 | func Test_GetSetString(t *testing.T) { 67 | store := getStore() 68 | defer clearStore(store) 69 | 70 | store.SetString("test", "abcd") 71 | value := store.String("test") 72 | if value != "abcd" { 73 | t.Errorf("Expected 'abcd', got '%s'", value) 74 | } 75 | 76 | store.SetString("test", "1234") 77 | value = store.String("test") 78 | if value != "1234" { 79 | t.Errorf("Expected '1234', got '%s'", value) 80 | } 81 | 82 | store.db.Close() 83 | 84 | defer panicHandler(t, "String: database is closed") 85 | store.String("test") 86 | 87 | defer panicHandler(t, "SetString: database is closed") 88 | store.SetString("test", "panic") 89 | } 90 | 91 | func Test_GetSetInt(t *testing.T) { 92 | store := getStore() 93 | defer clearStore(store) 94 | 95 | store.SetInt("test", 1234) 96 | i := store.Int("test") 97 | if i != 1234 { 98 | t.Errorf("Expected 1234, got %d", i) 99 | } 100 | 101 | i = store.Int("doesntexist") 102 | if i != 0 { 103 | t.Errorf("Expected 0, got %d", i) 104 | } 105 | 106 | store.SetString("test", "abcd") 107 | defer panicHandler(t, "Int: not a number") 108 | store.Int("test") 109 | } 110 | 111 | func Test_GetSetFloat(t *testing.T) { 112 | store := getStore() 113 | defer clearStore(store) 114 | 115 | store.SetFloat("test", 1234.567) 116 | f := store.Float("test") 117 | if f != 1234.567 { 118 | t.Errorf("Expected 1234.567, got %f", f) 119 | } 120 | 121 | f = store.Float("doesntexist") 122 | if f != 0 { 123 | t.Errorf("Expected 0, got %f", f) 124 | } 125 | 126 | store.SetString("test", "abcd") 127 | 128 | defer panicHandler(t, "Float: not a number") 129 | store.Float("test") 130 | } 131 | 132 | func Test_GetSetBool(t *testing.T) { 133 | store := getStore() 134 | defer clearStore(store) 135 | 136 | b := store.Bool("nothere") 137 | if b { 138 | t.Error("Expected false, got true") 139 | } 140 | 141 | store.SetBool("test", true) 142 | if !store.Bool("test") { 143 | t.Error("Expected true, got false") 144 | } 145 | 146 | store.SetBool("test", false) 147 | if store.Bool("test") { 148 | t.Error("Expected false, got true") 149 | } 150 | } 151 | 152 | func Test_GetSetTime(t *testing.T) { 153 | store := getStore() 154 | defer clearStore(store) 155 | 156 | v := store.Time("nothere") 157 | if !v.IsZero() { 158 | t.Errorf("Expected zero value, got %s", t) 159 | } 160 | 161 | now := time.Now() 162 | store.SetTime("test", now) 163 | v = store.Time("test") 164 | if v.Format(time.Stamp) != now.Format(time.Stamp) { 165 | t.Errorf("Expected %s, got %s", now, v) 166 | } 167 | 168 | store.SetString("test", "not a date") 169 | 170 | defer panicHandler(t, "Time: not a date") 171 | store.Time("test") 172 | } 173 | 174 | func Test_Del(t *testing.T) { 175 | store := getStore() 176 | defer clearStore(store) 177 | 178 | defer noPanicHandler(t, "Del: deleting non-existant key") 179 | store.Del("blabla") 180 | 181 | store.SetString("test", "abcd") 182 | store.Del("test") 183 | 184 | value := store.String("test") 185 | if value != "" { 186 | t.Errorf("Expected '', got '%s'", value) 187 | } 188 | 189 | store.db.Close() 190 | defer panicHandler(t, "Del: database is closed") 191 | store.Del("test") 192 | } 193 | 194 | func Test_HasKey(t *testing.T) { 195 | store := getStore() 196 | defer clearStore(store) 197 | 198 | if store.HasKey("test") { 199 | t.Error("Expected false, got true") 200 | } 201 | 202 | store.SetString("test", "abcd") 203 | if !store.HasKey("test") { 204 | t.Error("Expected true, got false") 205 | } 206 | 207 | store.SetString("test", "") 208 | if !store.HasKey("test") { 209 | t.Error("Expected true, got false") 210 | } 211 | 212 | store.Del("test") 213 | if store.HasKey("test") { 214 | t.Error("Expected false, got true") 215 | } 216 | } 217 | 218 | func Test_All(t *testing.T) { 219 | store := getStore() 220 | defer clearStore(store) 221 | 222 | store.SetString("test", "abcd") 223 | store.SetInt("num", 1234) 224 | store.SetBool("boolean", true) 225 | 226 | all := store.All() 227 | if len(all) != 3 { 228 | t.Errorf("Expected 3 rows, got %d", len(all)) 229 | } 230 | 231 | expected := []SqlKvRow{ 232 | SqlKvRow{Name: "test", Value: "abcd"}, 233 | SqlKvRow{Name: "num", Value: "1234"}, 234 | SqlKvRow{Name: "boolean", Value: "1"}, 235 | } 236 | 237 | for _, expectedKv := range expected { 238 | var found bool 239 | for _, kv := range all { 240 | if kv.Name == expectedKv.Name && kv.Value == expectedKv.Value { 241 | found = true 242 | break 243 | } 244 | } 245 | if !found { 246 | t.Error("Not found:", expectedKv) 247 | } 248 | } 249 | 250 | store.Clear() 251 | all = store.All() 252 | if len(all) != 0 { 253 | t.Errorf("Expected 0 rows") 254 | } 255 | } 256 | 257 | func Test_Placeholder(t *testing.T) { 258 | store := getStore() 259 | defer clearStore(store) 260 | 261 | if store.placeholder(0) != "?" || store.placeholder(1) != "?" { 262 | t.Error("Incorrect placeholder") 263 | } 264 | 265 | store.SetDriverName("postgres") 266 | if store.placeholder(1) != "$1" || store.placeholder(2) != "$2" { 267 | t.Error("Incorrect placeholder") 268 | } 269 | 270 | store.SetDriverName("sqlite3") 271 | if store.placeholder(0) != "?" || store.placeholder(1) != "?" { 272 | t.Error("Incorrect placeholder") 273 | } 274 | } 275 | 276 | func Test_DefaultValues(t *testing.T) { 277 | store := getStore() 278 | defer clearStore(store) 279 | 280 | b := store.BoolD("nothing", false) 281 | if b { 282 | t.Fatal() 283 | } 284 | 285 | b = store.BoolD("nothing", true) 286 | if !b { 287 | t.Fatal() 288 | } 289 | 290 | s := store.StringD("nothing", "something") 291 | if s != "something" { 292 | t.Fatal() 293 | } 294 | 295 | i := store.IntD("nothing", 123) 296 | if i != 123 { 297 | t.Fatal() 298 | } 299 | 300 | f := store.FloatD("nothing", 3.14) 301 | if f != 3.14 { 302 | t.Fatal() 303 | } 304 | 305 | defaultTime, _ := time.Parse("2006-Jan-02", "2013-Feb-03") 306 | tim := store.TimeD("nothing", defaultTime) 307 | if tim.Year() != 2013 || tim.Month() != 2 || tim.Day() != 3 { 308 | t.Fatal() 309 | } 310 | } 311 | --------------------------------------------------------------------------------