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