Q:

gob encoding to/decoding from *os.File not working

When I use a file pointer f *os.File I get a empty map back
func decode(f *os.File, b map[string]interface{}) error {
    err := gob.NewDecoder(f).Decode(&b)
    fmt.Printf("%+v\n", b)
    return err
}

func encode(f *os.File, b map[string]interface{}) error {
    bb := map[string]interface{}{
        "X":        1,
        "Greeting": "hello",
    }
    err := gob.NewEncoder(f).Encode(bb)
    f.Sync()
    //fmt.Println(buf.Bytes())
    return err
}

prints map[]
If I replace it with a global buffer pointer buf *bytes.Buffer it works
func decode(f *os.File, b map[string]interface{}) error {
    err := gob.NewDecoder(buf).Decode(&b)
    fmt.Printf("%+v\n", b)
    return err
}

func encode(f *os.File, b map[string]interface{}) error {
    bb := map[string]interface{}{
        "X":        1,
        "Greeting": "hello",
    }
    err := gob.NewEncoder(buf).Encode(bb)
    return err
}

prints map[Greeting:hello X:1]
f is opened in main like this 
var (
    buf      *bytes.Buffer
    f       *os.File
    memcache map[string]interface{}        
)

func main() {
    var err error
    f, err = os.Create("_memcache.txt")
    if err != nil {
        log.Print(err.Error())
    }
    defer f.Close() 

    memcache = make(map[string]interface{})
    buf = new(bytes.Buffer)
    gob.Register(map[string]interface{}{})
    err = write()
    if err != nil {
       log.Print(err.Error())
    }
    err = read()
    if err != nil {
        log.Print(err.Error())
    }
}

func read() (err error) {
    err = decode(f, memcache)
    if err != nil && err != io.EOF {
        return err
    }
}

func write() (err error) {
    err = encode(f, memcache)
    if err != nil {
        return err
    }
 }

A:

The solution is already mentioned in the comments, so the update method main() is following:
func main() {
    var err error
    f, err = os.Create("_memcache.txt")
    if err != nil {
        log.Print(err.Error())
    }
    defer f.Close()

    memcache = make(map[string]interface{})
    buf = new(bytes.Buffer)
    gob.Register(map[string]interface{}{})

    err = write()
    if err != nil {
        log.Print(err.Error())
    }

    // after write the file pointer is at the end of the file,
    // so it should be moved:      
    f.Seek(0, 0)

    // alternatively, open the file in read()
    err = read()
    if err != nil {
        log.Print(err.Error())
    }
}

