设为首页 加入收藏

TOP

Go Web:数据存储(2)——CSV文件(二)
2018-12-03 20:11:18 】 浏览:266
Tags:Web 数据 存储 CSV 文件
Post) // 模拟几篇文章 post1 := &Post{Id: 1, Content: "Hello 1", Author: "userA"} post2 := &Post{Id: 2, Content: "Hello 2", Author: "userB"} post3 := &Post{Id: 3, Content: "Hello 3", Author: "userC"} post4 := &Post{Id: 4, Content: "Hello 4", Author: "userA"} // 存储到内存中 store(post1) store(post2) store(post3) store(post4) // 将内存中的map容器,保存到csv文件中 storeToCsv("d:/a.csv", PostById) // 为了测试,此处将已保存在内存中的数据清空 PostById = map[int]*Post{} PostsByAuthor = map[string][]*Post{} // 下面是加载csv文件 s := load("d:/a.csv") for _, post := range s { store(post) } // 检索 fmt.Println(PostById[1]) fmt.Println(PostById[2]) for _, post := range PostsByAuthor["userA"] { fmt.Println(post) } for _, post := range PostsByAuthor["userC"] { fmt.Println(post) } }

运行结果:

&{1 Hello 1 userA}
&{2 Hello 2 userB}
&{1 Hello 1 userA}
&{4 Hello 4 userA}
&{3 Hello 3 userC}
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇104 - kube-scheduler源码分析 - .. 下一篇Go Web:数据存储(1)——内存存储

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目