Saving map to binary file

973 views
Skip to first unread message

RoboTamer

unread,
Aug 30, 2012, 6:13:27 PM8/30/12
to golan...@googlegroups.com

I am writing a wiki as exercise to learn Go
I have professional experience only in programming CNC and Robotics, and as hobby, Basic back from the days of Commodore C64 and PHP
I also know a little Python & Ruby.

But Go opens up a whole new world.
Since I don't wont any database dependency, I was going to save some data, I keep in memory to json, and loaded back at startup when it came to me that there must be a way to save the data as binary.
Some Googling resulted in C++ and .NET sample code, and I also found bits and pieces of Go, but I am not able to wrap my head around it so I was wondering if I can get some help.

I need to save a map that looks like following code . How would I go about that?

var Wikis map[int]jsonWiki

type jsonWiki struct {
    Title string
    File  string
    User  string
    //Groups []wikiGroups
    //Keys [5]string
    Time int64
}

//groups wikiGroups, , keys [5]string
func (p *jsonWiki) PutJsonWiki(title string, file string, user string) {
    p.Title = title
    p.File = file
    p.User = user
    //p.Groups = groups
    //p.Keys = keys
    p.Time = time.Now().Unix()
}

    Wikis = make(map[int]jsonWiki)
    addWiki("Lobby", "lobby.html", "admin")
    addWiki("Linux", "linux.html", "admin")


 

RoboTamer

unread,
Sep 1, 2012, 5:16:07 PM9/1/12
to golan...@googlegroups.com, toqu...@gmail.com

Thanks, that was great advice toqu....
For others:
The link to the slides is out of order but you can find the slides, and the code at https://github.com/nf/goto 
You will have to dick in to the history because Andrew Gerrand the coder switched at some point to json.
Would be nice to know why!  

On Thursday, August 30, 2012 5:39:42 PM UTC-7, toqu...@gmail.com wrote:
It seems you are looking for encoding/gob

That's the way to "go" when you want some go types saved to disk.

You can check the golang blog or check this wonderful video and slides from Andrew Gerrand ( found it on golang.org/doc/ ) that uses the gob package to create a url shortening service. It may need some minor go1 adjustments but it should suffice for your needs.

Hope it helps and high five!
Reply all
Reply to author
Forward
0 new messages