在Go语言中的加载和存储操作

huangapple go评论84阅读模式
英文:

Load and Store in Go Language

问题

我是新手,想知道在Go语言中是否有像Python中的pickle一样的方法来加载和存储预计算的变量。我的代码从一些数据中创建了一个映射和一个数组,我不想每次运行代码时都花时间计算它们。我希望下次运行代码时可以直接加载那个映射和数组。有人可以帮助我吗?

谢谢! 在Go语言中的加载和存储操作

英文:

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 在Go语言中的加载和存储操作

答案1

得分: 2

我不了解pickle的工作原理,如果你想将一个结构体转储到文件中,也许可以使用gob包,详细了解请参考https://stackoverflow.com/questions/12854125/how-do-i-dump-the-struct-into-the-byte-array-without-reflection

此外,我找到了一个可以读写Python的pickle的包https://github.com/hydrogen18/stalecucumber

英文:

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 https://stackoverflow.com/questions/12854125/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.

答案2

得分: 0

将您提供的内容翻译为中文如下:

一次计算并将所有变量保存在一个文件中,然后打开该文件并加载所有变量。当没有文件可打开时,表示第一次计算,因此计算并保存一次。您可以使用自己的文件格式,或者使用标准库(如"encoding/json"、"encoding/gob"、"encoding/csv"、"encoding/xml"等)。

以下是工作代码:

1- 使用"encoding/json",在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 {
		// 计算并保存一次:
		data := Data{[2]int{101, 102}, map[int]string{1: "Hello ", 2: "World."}}
		buf, err := json.Marshal(data)
		if err != nil {
			panic(err)
		}
		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",在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 {
		// 计算并保存一次:
		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- 有关protobuf,请参阅:https://stackoverflow.com/questions/37618399/efficient-go-serialization-of-struct-to-disk

英文:

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: https://stackoverflow.com/questions/37618399/efficient-go-serialization-of-struct-to-disk

答案3

得分: -1

或许 gob 包是最接近的:
https://golang.org/pkg/encoding/gob/

英文:

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

huangapple
  • 本文由 发表于 2016年10月26日 10:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/40252962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定