Golang not using a DB

huangapple go评论91阅读模式
英文:

Golang not using a DB

问题

我不想在这个时候使用数据库,我想将数据保存为结构体形式的文件。
我的问题是如何组织和检索数据。

例如,我有:

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(){
 //在这里创建Foos,并将它们放入全局数组:[]Foo ??
}

func GetFoo(name string) Foo{
 //这将搜索在MakeFoos()中填充的数组,并返回一个Foo ??
}

在Go中,将数据保存在内存中而不是数据库中,这样做可以吗?
欢迎提供任何帮助和建议,让我开始。

英文:

I don't want to use a DB at this time, I'd like to keep data, as structs, in a file.
My question is how to organize and retrieve the data.

For example, I have:

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

I want to make a bunch of different Foos and Bars, then I want to be able to query/select particular Foos and Bars.
Would I make a package then a file in the package, for instance, foo/foo.go; and in foo.go have a method that makes all of the Foos, then another method that would accept, for example, a name, and then search for that particular 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 ??
}

Is this an ok start to hold data in memory as opposed to a DB in Go?
Any help and suggestions to get me going welcomed.

答案1

得分: 1

我正在进行一个不使用数据库的Go项目。使用切片、映射、链表、JSON和操作系统文件系统,可以完成很多任务而无需使用数据库。我没有为你提供解决方案,但你可以研究一下我刚提到的工具。所有这些工具都包含在标准的Go库中。

英文:

I am working on a GO project that does not use a database. Using slices, maps, linked lists, json, and the OS file system quite a bit can be accomplished without a database. I don't have a solution for you, but study up on the tools I just mentioned. All these tools are included in the standard Go libraries.

huangapple
  • 本文由 发表于 2014年6月25日 01:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/24392418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定