1

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.

3
  • For fast searches of a lot of items, you need indexes, so you need a map per index that you maintain on insert/delete/update. To index and avoid data loss, you'll mostly end up implementing a (special-purpose) database yourself. In many applications, holding data in memory instead of a DB will cause you grief. Here's a post on getting started with the gorm ORM and SQLite, if that helps. Commented Jun 24, 2014 at 17:47
  • 3
    You may not want to use a database at this time - but you're going to spend the next few months implementing 5% of a databases features by hand and they will be nowhere near as efficient. I suggest you consider using a database right now to get up and running quicker. Commented Jun 24, 2014 at 22:34
  • sqlite is quite easy to use from GoLang and it doesn't involve installing a separate database... You can even have GoLang create the DB on startup (if it doesn't exist yet) and just start using it. The indexing and such will make your life a whole lot easier. Commented Mar 19, 2015 at 16:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.