FAQ
Hey goophers! I'm looking for some wisdom! I'm currently writing a program
with an in memory slice of pointers to structs (read "database" of sorts).
Two things I need your help, first and the most obvious I have a struct
with a lot of fields lets call it Thing, so I'm getting those Things from a
JSON file and Unmarshaling on startup. What is better? To parse into a
[]Things or a []*Things, As far as I can see the second one takes less
memory, and for that I'll need to explain the second point with a little
bit of code.

type Thing struct {
     .... // Lots of fields from strings to maps to bools
}

type Things []*Thing //Slice of Pointers for now...

Then I'm writing a lot of functions to search my slice ("database") in a
web app. And most of them look like this:

func (t Things) SearchByName(name, lang string) Things {
     results := make(Things, 0, DefaultCapacity) *// any idea on a good
value??*
     for _, thing := range(t) {
         *// Start of function specific code*
         if name.Partially.Matches.Or.Something(thing.Name) {
              results = append(results, thing)
         }
     }
     return results
}

func (t Things) SearchBySomeBool(some bool) Things {
     results := make(Things, 0, DefaultCapacity)
     for _, thing := range(t) {
         // Start of function specific code
         if thing.SomeBool == some {
              results = append(results, thing)
         }
     }
     return results
}

Etcetera... you get the point. Working like this is nice cause I can filter
my original Slice (database) and then use the same functions to filter the
resulting slice (as it is of type Things) and by that keep chaining
searches (filters) until I get what I want. And it's working nicely, but as
you can see there is a lot of repeated code (in blue) on each of those
functions. So I tried to refactor this into a single prototype function
(which will receive specific functions maybe?) to look like this.

func (t Things) Search(......) Things {
     results := make(Things, 0, DefaultCapacity)
     for _, thing := range(t) {
         // Start of function specific code
         // I'm thinking of receiving a function that takes a 'thing' and
does its stuff then returns true or false
         if function.YieldsTrue {
              results = append(results, thing)
         }
     }
     return results
}

Thing is, I could send the specific function to Search, and that function
must receive a Thing to check whether the thing matches the search and
return true or not and return false. But then I stomped into a wall!!! Each
SearchBy function receives different parameters (strings, bools, etc...) so
they can check specific matching. And it was all fine when I could chain my
searches from the top cause I had those parameters at hand and I could send
them to each function separately.

Anyone knows how to refactor this so I could use a single Search func to
DRY the code a bit and make it more idiomatic? Maybe use of channels?? I
dunno, every bit of help is really appreciated.

Thanks a lot.
Guillermo Estrada



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 11 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 30, '14 at 3:11a
activeOct 4, '14 at 5:16a
posts11
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase