需求和思路
在一般的小项目或者一个小软件,例如客户端之类的小程序中,可能会需要数据的持久化.但是使用一般的数据库(Mysql)之类的不合适.使用sqlite3这种嵌入式的是个较好的方法,但是Go语言中sqlite3的库是C语言的,Cgo不支持跨平台编译.正是由于这种需求,才想到使用json格式将数据直接保存在文件中.
具体的思路是怎么样呢? 在Go语言中如果要将数据转化成json格式的话,有两种格式 struct 和 map. 如果同时需要增删查改功能的话,将map作为中间格式是比较合适的.接下来我们就来实现它.
查询操作
这种操作的实现比较简单,直接将文件中的数据读取出来,使用json库反序列化就可以了. 代码如下 :
type Product struct {
Name string `json:"name"`
Num int `json:"num"`
}
func findAll() {
ps := make([]Product, 0)
data, err := ioutil.ReadFile("./index.json")
if err != nil {
log.Fatal(err)
}
// 这里参数要指定为变量的地址
err = json.Unmarshal(data, &ps)
if err != nil {
log.Fatal(err)
}
fmt.Println(ps)
}
添加操作
添加的实现实在查询的基础上的,我们需要先查询文件中的数据库,并转化为map格式,再将struct也转化为map格式(这里要使用反射),合并map,json序列化,最后保存在文件中.代码如下:
func create() {
fields := make([]map[string]interface{}, 0)
p1 := &Product{
Name: "Blog",
Num: 2,
}
_, _ = json.Marshal(p1)
// 读取文件中的数据,保存为map格式
data, _ := ioutil.ReadFile("./index.json")
err := json.Unmarshal(data, &fields)
if err != nil {
log.Fatal(err)
}
// 使用反射将struct转化为map
tp := reflect.TypeOf(p1).Elem()
vp := reflect.ValueOf(p1).Elem()
field := make(map[string]interface{}, 0)
for i := 0; i < tp.NumField(); i++ {
field1 := tp.Field(i)
field2 := vp.Field(i)
key := field1.Tag.Get("json")
field[key] = field2.Interface()
}
// 合并map
fields = append(fields, field)
// 写入文件
out, _ := json.Marshal(fields)
_ = ioutil.WriteFile("./index.json", out, 0755)
}
条件查询
思路: 将struct转化为map,根据输入的条件查询.查询的结果转化为struct.代码如下:
func FindOne() {
product := &Product{}
p1 := &Product{
Name: "John",
Num: 23,
}
// 使用反射将struct转化为map
tp := reflect.TypeOf(p1).Elem()
vp := reflect.ValueOf(p1).Elem()
field := make(map[string]interface{}, 0)
for i := 0; i < tp.NumField(); i++ {
field1 := tp.Field(i)
field2 := vp.Field(i)
key := field1.Tag.Get("json")
switch field2.Kind() {
case reflect.Int:
field[key] = float64(field2.Interface().(int))
case reflect.Int8:
field[key] = float64(field2.Interface().(int8))
case reflect.Int16:
field[key] = float64(field2.Interface().(int16))
case reflect.Int32:
field[key] = float64(field2.Interface().(int32))
case reflect.Int64:
field[key] = float64(field2.Interface().(int64))
case reflect.Uint:
field[key] = float64(field2.Interface().(uint))
case reflect.Uint8:
field[key] = float64(field2.Interface().(uint8))
case reflect.Uint16:
field[key] = float64(field2.Interface().(uint16))
case reflect.Uint32:
field[key] = float64(field2.Interface().(uint32))
case reflect.Uint64:
field[key] = float64(field2.Interface().(uint64))
case reflect.Float32:
field[key] = float64(field2.Interface().(float32))
case reflect.Float64:
field[key] = field2.Interface()
default:
field[key] = field2.Interface()
}
}
_, _ = json.Marshal(p1)
// 读取文件中的数据,保存为map格式
// 数据转化为map时,数值类型的统一变成float64
data, _ := ioutil.ReadFile("./index.json")
fields := make([]map[string]interface{}, 0)
err := json.Unmarshal(data, &fields)
if err != nil {
log.Fatal(err)
}
// 查询的条件
columns := []string{"name", "num"}
length := len(columns)
for _, item := range fields {
for i := 0; i < length; i++ {
// 这里的比较需要改进
if item[columns[i]] != field[columns[i]] {
break