en Home Smtp Sometimes I fall in love with code jgdb A Gopher named Gophy GoWatch Cfg File Access and Create Time Go Snippets Ping Citadel vCard Sync Your Everyday Go Tools Cases Conv Go jkl Encode/Decode between struct and byte slice Crypt HDKeys Wordlist HDKeys Recover

Encode/Decode Golang data to Gob files

Save() and Load() any Go values to/from files

Golang has a package called Gob to encode and decode values in to go specific binary format for transmiting as streams or for saving to file.

Below are two functions, Load and Save to encode/decode go values including struct's in to files.


package main

import (
    "encoding/gob"
    "fmt"
    "os"
    "runtime"
)

const file = "/tmp/test.gob"

type User struct {
    Name, Pass string
}

func main(){
    var datato = &User{"Donald","DuckPass"}
    var datafrom = new(User)

    err := Save(file, datato)
    Check(err)
    err = Load(file, datafrom)
    Check(err)
    fmt.Println(datafrom)
}

// Encode via Gob to file
func Save(path string, object interface{}) error {
    file, err := os.Create(path)
    if err == nil {
        encoder := gob.NewEncoder(file)
        encoder.Encode(object)
    }
    file.Close()
    return err
 }

// Decode Gob file
func Load(path string, object interface{}) error {
    file, err := os.Open(path)
    if err == nil {
        decoder := gob.NewDecoder(file)
        err = decoder.Decode(object)
    }
    file.Close()
    return err
}

func Check(e error) {
    if e != nil {
        _, file, line, _ := runtime.Caller(1)
        fmt.Println(line, "\t", file, "\n", e)
        os.Exit(1)
    }
}


First Created: 2015-01-02T12:50:00Z
Last Modified: 2026-04-05 19:35