dsbpaqt61965 2014-03-08 13:05
浏览 14

直接在源代码中使用gobs,这可能吗?

I would like to know if it's possible to have gob encoded data directly in the source code (e.g. in a function). The reason is to increase performance by not having to access the disk to get the gob file. I'm aware of memcached, redis and friends. i don't need TTL or any other fancy feature. Just maps in memory. The data would be encoded and dumped in the source code in a 'setup'/build process so that at runtime it would only need to 'decode' it.

The go application would basically serve as a small read-only, embedded database. I can do this using json (basically declare a var with the raw json) but I guess there would be a performance penalty so I'm wondering if it's possible with gob.

I've tried different things but I can't make make it work because basically I don't know how to define the gob var (byte, [bytes] ?? ) and the decoder seems to expect an io.Reader so before spending the whole day on this I've decided to ask you SO fellas at least if it's possible to do.

Miserable attempt:

var test string
test = "hello"

p := new(bytes.Buffer)
e := gob.NewEncoder(p)
e.Encode(test)
ers := ioutil.WriteFile("test.gob", p.Bytes(), 0600)
if ers != nil {
    panic(ers)
}

Now I would like to take test.gob and add it in a function. As I can see, the source of test.gob reads like ^H^L^@^Ehello

var test string

var b bytes.Buffer

b = byte("^H^L^@^Ehello")

de := gob.NewDecoder(b.Bytes())

er := de.Decode(&test)
if er != nil {
    fmt.Printf("cannot decode")
    panic(er)
}

fmt.Fprintf(w, test)
  • 写回答

2条回答 默认 最新

  • duankui6150 2014-03-08 14:07
    关注

    Store the data in a byte slice. It's raw data, and that's how you would read it in from a file.

    The string in your gob file is not "^H^L^@^Ehello"! That's how your editor is displaying the non-printable characters.

    b = byte("^H^L^@^Ehello")
    // This isn't the string equivalent of hello, 
    // and you want a []byte, not byte. 
    // It should look like 
    
    b = []byte("\b\f\x00\x05hello")
    // However, you already declared b as bytes.Buffer, 
    // so this assignment isn't valid anyway.
    
    
    de := gob.NewDecoder(b.Bytes())
    // b.Bytes() returns a []byte, you want to read the buffer itself.
    

    Here's a working example http://play.golang.org/p/6pvt2ctwUq

    func main() {
        buff := &bytes.Buffer{}
        enc := gob.NewEncoder(buff)
        enc.Encode("hello")
    
        fmt.Printf("Encoded: %q
    ", buff.Bytes())
    
        // now if you wanted it directly in your source
        encoded := []byte("\b\f\x00\x05hello")
        // or []byte{0x8, 0xc, 0x0, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
    
        de := gob.NewDecoder(bytes.NewReader(encoded))
    
        var test string
        er := de.Decode(&test)
        if er != nil {
            fmt.Println("cannot decode", er)
            return
        }
    
        fmt.Println("Decoded:", test)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值