1. Error description
An error occurs when using gob serialization: gob: addressable value of type * big. Float
Scenario:
Serializing a data structure contains a map field, where value is the big. Float
structure type
type SideBlock {
...
Varphi map[string]big.Float
...
}
func (b *SideBlock) Serialization() []byte {
var res bytes.Buffer
encoder := gob.NewEncoder(&res)
err := encoder.Encode(b)
if err != nil {
logger.Error("Serialization err : ", err)
}
return res.Bytes()
}
Go version: go version go1.16.5 Darwin/arm64
OS: Mac OS BigSur 11.5.2
2. Error reason
The answer is found in issue:
He has a problem using URL. URL
as the key of map
The problem lies in the pointer receiver of marshalbinary
. If we want to use this structure as a key or value, we need to implement the marshalbinary
method for special fields
personal understanding: when a map field of a structure has another structure instead of a pointer to the structure, it is a copy of the structure rather than itself during serialization, so it is difficult to address </ font>
3. Solution
Method 1:
https://play.golang.org/p/dK7SsqahtAd
package main
import (
"encoding/gob"
"fmt"
"io/ioutil"
)
type T struct {
}
func (t *T) String() string { return "" }
func (t T) MarshalBinary() (text []byte, err error) {
return []byte(t.String()), nil
}
func main() {
m := make(map[T]int)
m[T{}] = 0
e := gob.NewEncoder(ioutil.Discard)
fmt.Println(e.Encode(m))
}
Method 2:
Change big. Float
into a pointer, that is, the field becomes varphi map [string] * big. Float
Read More:
- [Solved] .\main.go:3:6: missing function body .\main.go:4:1: syntax error: unexpected semicolon or newline be
- Implement base64_decode in GO language to solve the problem of illegal characters
- Go Language Error: main redeclared in this block [How to Solve]
- Go declares that the local variable does not use command line arguments. Main. Go: 4:6: a declared but not used
- [Solved] Error running ‘go build myfir_go_project‘: Cannot run program
- [Solved] Golang Error: main.go:5:2: package go_code/chapter10/factory/model is not in GOROOT
- [Solved] org.apache.spark.SparkException: Kryo serialization failed: Buffer overflow
- [Go] Solve panic: runtime error: invalid memory address or nil pointer dereference in golang
- [Solved] Compilation error: dereferencing pointer to incomplete type…
- Docker run Error: container_linux.go:235: starting container process caused “process_linux.go:258: appl
- GO Exception Runnerw.exe: CreateProcess failed with error 21
- [Solved] panic: runtime error: invalid memory address or nil pointer dereference
- std::dynamic_pointer_cast Error: source type is not polymorphic
- Go Slice Error: panic:runtime error:index out of range [0] with length 0 [Solved]
- [Go] Testing when solving go test: warning: no tests to run
- [Solved] Docker Startup Error: panic: runtime error: invalid memory address or nil pointer dereference
- [Solved] go cobra Error: required flag(s) “pkg-name” not set
- IE8 browser Webloader will always go uploaderror and report error: http
- [Solved] VScode Error: build constraints exclude all Go files in syscall\js
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”