Comments (4)
Below is the demo code. It cannot work.
package main
import (
"encoding/gob"
"fmt"
"log"
"os"
)
type Node struct {
Val int
Prev *Node
Next *Node
}
func main() {
// Creating doubly linked list
head := &Node{Val: 1}
head.Next = &Node{Val: 2, Prev: head}
head.Next.Next = &Node{Val: 3, Prev: head.Next}
// Create and open file to store data
file, err := os.Create("store.gob")
if err != nil {
log.Fatal("create file error:", err)
}
defer file.Close()
// Create encoder and encode structure to file
enc := gob.NewEncoder(file)
if err = enc.Encode(head); err != nil {
log.Fatal("encode error:", err)
}
// Close the file
file.Close()
// Open file to be ready for decoding
file, err = os.Open("store.gob")
if err != nil {
log.Fatal("open file error:", err)
}
defer file.Close()
// Create decoder and decode structure from file
var decodedHead Node
dec := gob.NewDecoder(file)
if err = dec.Decode(&decodedHead); err != nil {
log.Fatal("decode error:", err)
}
// Printing decoded data
for node := &decodedHead; node != nil; node = node.Next {
fmt.Println(node.Val)
}
}
from go.
The singly linked list works perfect.
package main
import (
"encoding/gob"
"fmt"
"log"
"os"
)
type Node struct {
Val int
Next *Node
}
func main() {
// Creating singly linked list
head := &Node{Val: 1}
head.Next = &Node{Val: 2}
head.Next.Next = &Node{Val: 3}
// Create and open file to store data
file, err := os.Create("store.gob")
if err != nil {
log.Fatal("creation file error:", err)
}
defer file.Close()
// Create encoder and encode structure to file
enc := gob.NewEncoder(file)
if err = enc.Encode(head); err != nil {
log.Fatal("encoding error:", err)
}
// Close the file
file.Close()
// Open file for decoding
file, err = os.Open("store.gob")
if err != nil {
log.Fatal("opening file error:", err)
}
defer file.Close()
// Create decoder and decode structure from file
var decodedHead Node
dec := gob.NewDecoder(file)
if err = dec.Decode(&decodedHead); err != nil {
log.Fatal("decoding error:", err)
}
// Printing the decoded list
for node := &decodedHead; node != nil; node = node.Next {
fmt.Println(node.Val)
}
}
from go.
from the package docs
recursive values (data with cycles) are problematic.
from go.
thanks for your reply.
from go.
Related Issues (20)
- x/net/webdav: PUT with missing intermediate collection gave 404, MUST be 409 [RFC4918:S9.7.1]) HOT 1
- net/http,x/crypto/ssh: Hijack hangs when underlying net.Conn doesn't support SetReadDeadline
- encoding/json: adding a MarshalJSON method confuses JSON marshalling HOT 4
- x/pkgsite: package removal request for https://pkg.go.dev/github.com/RectangelHelp/blockaid-client-go HOT 1
- x/tools/gopls: "View package documentation": use type information
- x/tools/gopls: make Hover display std version information HOT 1
- cmd/compile: SIGBUS unaligned access on mips64 via qemu-mips64 HOT 7
- proposal: x/tools/go/types/typeutil: add TypeMap[V] HOT 11
- x/net/ipv4: TestRawConnReadWriteUnicastICMP failures HOT 1
- x/pkgsite: package removal request for https://pkg.go.dev/github.com/spywiree/languagecodes
- cmd/compile: SIGBUS unaligned access on mips64 via qemu-mips64 [1.21 backport] HOT 6
- x/build: modify contents of "gray box" (summary markdown) in LUCI UI to be more directly to the point for test failures HOT 2
- x/net/http2/h2c panic HOT 2
- Reflect package for protobuf values HOT 4
- Proposal #43947 was accepted and implemented for this instead.
- compress/gzip: cannot decompress files containing large gzip header comments HOT 1
- Can't install Gopls in Linux mint HOT 1
- x/text/transform: more examples
- proposal: runtime: detect calls to fork in C code (by observing PID change after cgocall return)
- cmd/link: missing MachoPlatform for Mac Catalyst and missing INODE64 extname for syscalls (fdopendir, readdir_r, getfsstat) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.