FAQ
I want to save/load data on gob file, so I Created "example.gob" and try to
save custom struct.. and It fail


package main

import (
     "encoding/gob"
     "os"
)

type Person struct {
     Title string
}
type Persons []Person

func (p *Persons) add(pp Person) {
     *p = append(*p, Person{pp.Title})
}

func IdxSave( page Persons) {
     f, err := os.Open("./example.gob")
     if err != nil {
         panic("cant open file")
     }
     defer f.Close()

     enc := gob.NewEncoder(f)
     if err := enc.Encode(page); err != nil {
         panic("cant encode")
     }
}

func main() {
      IdxSave(Persons{Person{"Hans"}})
}


when I see the log. it say " bad file descriptor" and some go routine which
I can't understand
how can I solved?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dustin at Sep 9, 2015 at 5:26 pm
    You're opening the file with os.Open which is read-only. You'll probably
    want https://golang.org/pkg/os/#Create
    On Wednesday, September 9, 2015 at 9:45:47 AM UTC-7, moonbi Ga wrote:

    I want to save/load data on gob file, so I Created "example.gob" and try
    to save custom struct.. and It fail


    package main

    import (
    "encoding/gob"
    "os"
    )

    type Person struct {
    Title string
    }
    type Persons []Person

    func (p *Persons) add(pp Person) {
    *p = append(*p, Person{pp.Title})
    }

    func IdxSave( page Persons) {
    f, err := os.Open("./example.gob")
    if err != nil {
    panic("cant open file")
    }
    defer f.Close()

    enc := gob.NewEncoder(f)
    if err := enc.Encode(page); err != nil {
    panic("cant encode")
    }
    }

    func main() {
    IdxSave(Persons{Person{"Hans"}})
    }


    when I see the log. it say " bad file descriptor" and some go routine
    which I can't understand
    how can I solved?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 9, '15 at 4:45p
activeSep 9, '15 at 5:26p
posts2
users2
websitegolang.org

2 users in discussion

moonbi Ga: 1 post Dustin: 1 post

People

Translate

site design / logo © 2021 Grokbase