1

I am new to Go and was wondering if there is a way to load and store precomputed variables in Go like pickle in Python.
My code is creating a map and an array from some data and I don't want to spend time in computation of those, every time the code runs.
I want to load that map and array directly next time I run the code.
Can someone help me with this?

TIA :)

Parag
  • 655
  • 4
  • 7
  • 19
  • 1
    Too short for answer: https://godoc.org/encoding/gob. See also: http://stackoverflow.com/questions/38129076/is-it-possible-to-pickle-instances-of-structs-in-golang – Charlie Tumahai Oct 26 '16 at 03:00

3 Answers3

2

I don't know about how pickle work, if you want to dump a struct into file, may be you can use gob package, see more detail for this How do I dump the struct into the byte array without reflection?

Also, I found a package that can read and write Python's pickle https://github.com/hydrogen18/stalecucumber.

halfer
  • 19,824
  • 17
  • 99
  • 186
Laily
  • 4,024
  • 1
  • 14
  • 14
0

Calculate your variables once and save them all once in a file,
then open that file and load them all.
When there is no file to open, it is the first time, so calculate and save it once.
You may use your own file format, if you like, or use standard library, like "encoding/json", "encoding/gob", "encoding/csv", "encoding/xml", ....


This:

data := calcOnce()

reads file:

rd, err := ioutil.ReadFile(once)

and if there is no error loads all the variables, otherwise calculates and saves them once.

Here's the working code:

1- Using "encoding/json", try it on The Go Playground:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

type Data struct {
    A [2]int
    B map[int]string
}

func main() {
    data := calcOnce()

    fmt.Println(data) // {[101 102] map[1:Hello  2:World.]}
}

func calcOnce() Data {
    const once = "date.json"
    rd, err := ioutil.ReadFile(once)
    if err != nil {
        //calc and save once:
        data := Data{[2]int{101, 102}, map[int]string{1: "Hello ", 2: "World."}}
        buf, err := json.Marshal(data)
        if err != nil {
            panic(err)
        }
        //fmt.Println(string(buf))
        err = ioutil.WriteFile(once, buf, 0666)
        if err != nil {
            panic(err)
        }
        return data
    }
    var d *Data
    err = json.Unmarshal(rd, &d)
    if err != nil {
        panic(err)
    }
    return *d
}

2- Using "encoding/gob", try it on The Go Playground:

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    "io/ioutil"
)

type Data struct {
    A [2]int
    B map[int]string
}

func main() {
    data := calcOnce()

    fmt.Println(data) // {[1010 102] map[2:World. 1:Hello ]}
}

func calcOnce() Data {
    const once = "date.bin"
    rd, err := ioutil.ReadFile(once)
    if err != nil {
        //calc and save once:
        data := Data{[2]int{101, 102}, map[int]string{1: "Hello ", 2: "World."}}
        buf := &bytes.Buffer{}
        err = gob.NewEncoder(buf).Encode(data)
        if err != nil {
            panic(err)
        }
        err = ioutil.WriteFile(once, buf.Bytes(), 0666)
        if err != nil {
            panic(err)
        }
        return data
    }
    var d Data
    err = gob.NewDecoder(bytes.NewReader(rd)).Decode(&d)
    if err != nil {
        panic(err)
    }
    return d
}

3- for protobuf see: Efficient Go serialization of struct to disk

Community
  • 1
  • 1
-1

Perhaps the gob package is closest: https://golang.org/pkg/encoding/gob/

md2perpe
  • 3,372
  • 2
  • 18
  • 22