gpt4 book ai didi

database - Golang 不使用数据库

转载 作者:IT王子 更新时间:2023-10-29 02:17:15 26 4
gpt4 key购买 nike

我现在不想使用数据库,我想将数据作为结构保存在文件中。我的问题是如何组织和检索数据。

例如,我有:

type Foo struct{
Id int
Name string
}
type Bar struct{
Id int
Name string
}

我想制作一堆不同的 Foos 和 Bars,然后我希望能够查询/选择特定的 Foos 和 Bars。我会制作一个包,然后在包中创建一个文件,例如 foo/foo.go;在 foo.go 中有一个生成所有 Foos 的方法,然后是另一个接受名称的方法,然后搜索那个特定的 Foo。

func MakeFoos(){
//make the Foos here, and put them in a global array: []Foo ??
}

func GetFoo(name string) Foo{
//this would search the array populated in MakeFoos() and return a Foo ??
}

与 Go 中的数据库相比,将数据保存在内存中是否合适?欢迎任何有助于我前进的帮助和建议。

最佳答案

我正在做一个不使用数据库的 GO 项目。使用 slice 、映射、链表、json 和 OS 文件系统可以在没有数据库的情况下完成很多工作。我没有适合您的解决方案,但请研究我刚才提到的工具。所有这些工具都包含在标准 Go 库中。

关于database - Golang 不使用数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24392418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com