2016-10-26 3 views
0

Ich bin neu in Go und habe mich gefragt, ob es eine Möglichkeit gibt, vorberechnete Variablen in Go wie Pickle in Python zu laden und zu speichern.
Mein Code erstellt eine Karte und ein Array aus einigen Daten und ich möchte keine Zeit in die Berechnung dieser verbringen, jedes Mal, wenn der Code ausgeführt wird.
Ich möchte diese Karte und Array direkt beim nächsten Mal den Code ausführen.
Kann mir jemand dabei helfen?In Go laden und speichern

TIA :)

+1

Zu kurz für die Antwort: https://godoc.org/encoding/gob. Siehe auch: http://stackoverflow.com/questions/38129076/is-it-possible-to-pickle-instances-of-structs-in-golang –

Antwort

0

Ihre Variablen einmal ermitteln und sie alle einmal in einer Datei speichern,
dann diese Datei öffnen und laden sie alle.
Wenn keine Datei zu öffnen ist, ist es das erste Mal, also berechnen und speichern Sie es einmal.
Sie können Ihr eigenes Dateiformat verwenden, wenn Sie mögen, oder standard library verwenden, wie "encoding/json", "encoding/gob", "encoding/csv", "encoding/xml", ....


Dieses:

data := calcOnce() 

liest Datei:

rd, err := ioutil.ReadFile(once) 

und wenn kein Fehler auftritt, werden alle Variablen geladen, andernfalls e berechnet und speichert sie einmal.

Hier ist der Arbeitscode:

1- "encoding/json" verwenden, versuchen Sie es auf 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- "encoding/gob" verwenden, versuchen Sie es auf 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- für protobuf siehe: Efficient Go serialization of struct to disk