On Fri, Apr 18, 2014 at 05:15:58AM -0700, Michael Rivera wrote:
> I know that this may be cumbersome but I'm just using this to see the
> capabilities of what the encoding/csv package can do. I know that the
> search can be done but I'm wondering how the search may work. Like lets say
> I search for the ID "123" do I take the 123 and just use it to compare the
> data line by line?
Yes. There is no other way without some kind of index or other preprocessing
step.
> If so how would I be comparing it? I mean how would I
> extract data from the csv in knowing which is which?
OUT:
for {
record, err = csv.Read()
switch {
case err == io.EOF:
break OUT
case err != nil:
log.Fatal(err)
case record[0] == "123":
//found
}
}
If you're only looking for one value, then 'break OUT' after finding it.
Obviously you'd have to convert types, etc if you're not just comparing
strings.
> For the delete function, basically it would be like a new write function? A
> write function that would create a new csv. My question here would be is if
> I overwrite the data say I write it over under the same file name, will it
> still retain the "not deleted" data?
You should not "write it over under the same file name". You could lose data.
Instead, you create a new file, under a different name and write to that.
When the writing is done, you rename the new file to the old file, replacing
the old data with the new data. Like I said, it is cumbersome.
In any case, it sounds like you'd want to use the code above as a function
returning a record number or file offset so you can use it for both
operations.
> I'm sorry for the weird questions sir
They are not weird at all, but it would be easier to help you if you show
the code you've written, even if it doesn't work. Otherwise, it's a bit of a
guessing game, especially since I don't know where you're heading.
-Gyepi