Created
October 26, 2016 04:25
-
-
Save imiskolee/4dbebb881deb0121dc881dda5af13fbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"errors" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
//一行数据库记录 | |
type Row struct { | |
ID int | |
Name string | |
Age int | |
Description string | |
} | |
//一张数据表 | |
type Table struct { | |
data []Row | |
AutoIncrementID int | |
} | |
func initTableData() []Row { | |
return make([]Row, 0) | |
} | |
func NewTable() *Table { | |
table := new(Table) | |
table.data = initTableData() | |
return table | |
} | |
func (table *Table) Insert(row *Row) error { | |
table.AutoIncrementID++ | |
row.ID = table.AutoIncrementID | |
table.data = append(table.data, *row) | |
return nil | |
} | |
func (table *Table) Delete(id int) error { | |
found := false | |
for k, v := range table.data { | |
if v.ID == id { | |
found = true | |
if k < len(table.data) { | |
table.data = append(table.data[:k], table.data[k+1:]...) | |
} else { | |
table.data = append(table.data[:k]) | |
} | |
} | |
} | |
if found { | |
return nil | |
} | |
return errors.New("No Rows Found.") | |
} | |
func (table *Table) FlushAll() error { | |
table.data = initTableData() | |
return nil | |
} | |
func (table *Table) GetAll() []Row { | |
return table.data | |
} | |
//命令的签名契约 | |
type HandleFunc func(*Table, string) error | |
//注册的命令列表 | |
var commandHandle map[string]HandleFunc | |
//当前打开的表 | |
var globalTable *Table | |
/** | |
Input 模块,用于监听用户输入,并且进行命令分发。 | |
**/ | |
func InputLoop() { | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
line := scanner.Text() | |
arr := strings.Split(line, " ") | |
handle, ok := commandHandle[strings.ToLower(arr[0])] | |
if !ok { | |
fmt.Println("unsupported command:", arr[0]) | |
continue | |
} | |
err := handle(globalTable, strings.Trim(line[len(arr[0]):], " ")) | |
if err != nil { | |
fmt.Println("Command Get Errors:", err.Error()) | |
} else { | |
fmt.Println("Operator OK.") | |
} | |
} | |
} | |
func Add(table *Table, input string) error { | |
arr := strings.Split(input, ",") | |
if len(arr) == 1 { | |
return errors.New("输入格式不正确") | |
} | |
age, err := strconv.Atoi(arr[1]) | |
if err != nil { | |
return errors.New("输入格式不正确") | |
} | |
newRow := Row{ | |
Name: arr[1], | |
Age: age, | |
Description: arr[2], | |
} | |
return table.Insert(&newRow) | |
} | |
func Delete(table *Table, input string) error { | |
id, err := strconv.Atoi(input) | |
if err != nil { | |
return errors.New("输入格式不正确" + input) | |
} | |
return table.Delete(id) | |
} | |
func ShowList(table *Table, input string) error { | |
pFmt := "%-5d %-10s %-3d %-32s\n" | |
fmt.Printf("%-5s %-10s %-3s %-32s\n", "id", "name", "age", "description") | |
fmt.Println() | |
for _, v := range table.GetAll() { | |
fmt.Printf(pFmt, v.ID, v.Name, v.Age, v.Description) | |
} | |
return nil | |
} | |
func FlushAll(table *Table, input string) error { | |
return table.FlushAll() | |
} | |
func Exit(table *Table, input string) error { | |
os.Exit(1) | |
return nil | |
} | |
func Help(table *Table, input string) error { | |
helper := | |
` | |
---------------------------------------------------------------------------------- | |
====================== Weixinhost Developer Database ============================= | |
---------------------------------------------------------------------------------- | |
help: | |
add name,age,descriotion 添加一条数据 (eg:add MiskoLee,26,Nice) | |
delete id 根据Id删除一条数据 (eg: delete 1) | |
flushall 清空数据表 | |
exit 退出程序 | |
` | |
fmt.Println(helper) | |
return nil | |
} | |
func init() { | |
commandHandle = make(map[string]HandleFunc, 0) | |
commandHandle["add"] = Add | |
commandHandle["delete"] = Delete | |
commandHandle["showlist"] = ShowList | |
commandHandle["flushall"] = FlushAll | |
commandHandle["exit"] = Exit | |
commandHandle["help"] = Help | |
} | |
func main() { | |
Help(nil, "") | |
globalTable = NewTable() | |
InputLoop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment