github.com/enetx/g@v1.0.80/examples/files/file_encode_decode.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func main() {
    10  	// Encoding and decoding using Gob
    11  
    12  	// Encode a slice of integers to a Gob file
    13  	g.NewFile("test.gob").Enc().Gob(g.SliceOf(1, 2, 3)).Unwrap()
    14  
    15  	// Decode the Gob file into a new slice of integers
    16  	var gobdata g.Slice[int]
    17  	g.NewFile("test.gob").Dec().Gob(&gobdata)
    18  
    19  	// Print the decoded Gob data
    20  	fmt.Println(gobdata)
    21  
    22  	// Encoding and decoding using JSON
    23  
    24  	// Encode a slice of integers to a JSON file
    25  	g.NewFile("test.json").Enc().JSON(g.SliceOf(1, 2, 3)).Unwrap()
    26  
    27  	// Decode the JSON file into a new slice of integers
    28  	var jsondata g.Slice[int]
    29  	g.NewFile("test.json").Dec().JSON(&jsondata)
    30  
    31  	// Print the decoded JSON data
    32  	fmt.Println(jsondata)
    33  }