go简单模拟Redis数据库对应{key, value}的存取功能
源代码:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 type M_Rcd map[string]string 8 9 type T_Tbl struct { 10 tblname string 11 record M_Rcd 12 } 13 14 type T_Kvdb struct { 15 dbname string 16 tbl []T_Tbl 17 } 18 19 func (this *T_Kvdb) createDB(db T_Kvdb) { 20 this.dbname = db.dbname 21 this.tbl = make([]T_Tbl, 5) 22 fmt.Printf("\ncreate db \"%s\" success.\n", this.dbname) 23 } 24 25 func (this *T_Kvdb) newTbl(tblname string) T_Tbl { 26 var tbl T_Tbl 27 var length int 28 tbl.tblname = tblname 29 tbl.record = make(M_Rcd) 30 31 this.tbl = append(this.tbl, tbl) 32 length = len(this.tbl) 33 fmt.Printf("add new table \"%s\" to db \"%s\" success.\n", tblname, this.dbname) 34 return this.tbl[length-1] 35 } 36 37 /* 38 * 思路:循环检索数据库是否有该表,如果有该表,则进一步检索是否有该Key,如有则输出 39 */ 40 func (this *T_Kvdb) read(key string, tbl T_Tbl) { 41 for i, _ := range this.tbl { 42 if this.tbl[i].tblname == tbl.tblname { 43 for k, v := range this.tbl[i].record { 44 if key == k { 45 fmt.Printf("find result : {%s}\n", v) 46 return 47 } 48 } 49 } 50 } 51 fmt.Printf("have no table: \"%s\"\n", tbl.tblname) 52 } 53 54 /* 55 * 对表进行写数据 56 * 参数: 57 * key: 键值 58 * value: 键值对应的数据 59 * tbl: 需要写如数据的表 60 */ 61 func (this *T_Kvdb) write(key string, value string, tbl T_Tbl) { 62 fmt.Printf("add {%s, %s} to table \"%s\"", key, value, tbl.tblname) 63 for i, _ := range this.tbl { 64 if this.tbl[i].tblname == tbl.tblname { 65 this.tbl[i].record[key] = value /* 对Table写入数据 */ 66 fmt.Println(" success.") 67 return 68 } 69 } 70 71 fmt.Println("have no table: ", tbl.tblname) 72 } 73 74 /* 75 * 删除表中key对应的{key, value} 76 */ 77 func (this *T_Kvdb) delete(key string, tbl T_Tbl) { 78 79 for i, _ := range this.tbl { 80 if this.tbl[i].tblname == tbl.tblname { 81 for k, _ := range this.tbl[i].record { 82 if key == k { 83 delete(this.tbl[i].record, key) // 删除key对应的数据{key, value} 84 fmt.Printf("delete key success.\n") 85 this.show() 86 return 87 } 88 } 89 } 90 } 91 92 fmt.Printf("have no table: %s\n", tbl.tblname) 93 } 94 95 /* 96 * 显示DB里所有Table的数据:{key, value} 97 */ 98 func (this *T_Kvdb) show() { 99 fmt.Print("show [") 100 for i, _ := range this.tbl { 101 for key, value := range this.tbl[i].record { 102 fmt.Print("{", key, ", ", value, "}") 103 } 104 } 105 106 fmt.Print("]\n") 107 } 108 109 /* 110 * 销毁表的思路: 111 * 如有表数组[5]={表0,表1,表2,表3,表4},现在需要删除{表2},则可以利用append函数重新组合表数组 112 * 来达到删除{表2}. 重组表数组的语句为:append(表头到表2(此处是开区间),(此处是闭区间)表3到表尾) 113 */ 114 func (this *T_Kvdb) destroy(tbl T_Tbl) { 115 for i, _ := range this.tbl { 116 if this.tbl[i].tblname == tbl.tblname { 117 this.tbl = append(this.tbl[:i], this.tbl[i+1:]...) 118 fmt.Printf("destroy table success.\n") 119 return 120 } 121 } 122 fmt.Printf("have no table: \n", tbl.tblname) 123 } 124 125 func main() { 126 var db T_Kvdb 127 var tbl T_Tbl 128 129 db.dbname = "student_management_system" 130 db.createDB(db) 131 tbl = db.newTbl("student") 132 db.write("hezhixiong", "shanghai", tbl) 133 db.write("zhangsan", "beijing", tbl) 134 db.show() 135 db.read("hezhixiong", tbl) 136 db.delete("hezhixiong", tbl) 137 fmt.Printf("db size = %d\n", len(db.tbl)) 138 db.destroy(tbl) 139 fmt.Printf("db size = %d\n", len(db.tbl)) 140 }
程序输出:
1 create db "student_management_system" success. 2 add new table "student" to db "student_management_system" success. 3 add {hezhixiong, shanghai} to table "student" success. 4 show [{hezhixiong, shanghai}] 5 find result : {shanghai} 6 delete key success. 7 show [] 8 db size = 6 9 destroy table success. 10 db size = 5