首页 > 其他分享 >Go - Decoding gob Format Data to Structs

Go - Decoding gob Format Data to Structs

时间:2023-10-05 10:00:48浏览次数:29  
标签:err Format read gob file Go reading data

Problem: You want to decode gob format data back to structs.


Solution: Use the encoding/gob package to decode the gob format data back to structs.

 

func   read ( data   interface {},   filename   string )   { 
      file ,   err   :=   os . Open ( "reading" ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  read  file:" ,   err ) 
      } 
      decoder   :=   gob . NewDecoder ( file ) 
      err   =   decoder . Decode ( data ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  decode  data:" ,   err ) 
      } 
}

Open the file named reading , which you created from the previous recipe. This file will be your Reader . You will create a decoder around the reader and then call Decode on it, passing the struct instance to be populated with the data.
Call the read function and pass in a reference to a struct instance:

read ( & reading ,   "reading" )

The reading struct instance will be populated with the data after the call.

Encoding gob, as you can see, is faster than encoding JSON, though the amount of memory used is the same. Decoding gob is much faster than decoding JSON as well and uses a lot less memory.

 

标签:err,Format,read,gob,file,Go,reading,data
From: https://www.cnblogs.com/zhangzhihui/p/17743101.html

相关文章

  • Go - Encoding Data to gob Format Data
    Problem: Youwanttoencodestructsintobinarygobformat.Solution: Usetheencoding/gobpackagetoencodethestructsintobytesthatcanbestoredorsentelsewhere. Theencoding/gobpackageisaGolibrarytoencodeanddecodeabinaryformat.The......
  • 工具 | 极其方便的谷歌翻译软件 Myna for Google Translate for Mac | Mac
    工具|极其方便的谷歌翻译软件MynaforGoogleTranslateforMac|Mac前言Mac哪款翻译软件好用呢?市面有太多的翻译工具了,如:百度、谷歌、有道等等。但是不得不说作为对外交流学习或学术阅览,谷歌翻译算得上是比较专业和让人信赖的。而MynaforGoogleTranslateforMac是......
  • MongoDB高阶特性:副本集、分片、事务、索引
    一、副本集(主从复制)1、docker-compose.ymlversion:'3'services:mongo1:image:mongocontainer_name:mongo1command:mongod--replSetrs0--port27017volumes:-./mongodb-cluster/mongod1:/data/dbports:-"27017:2......
  • 2023-10-04:用go语言,现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你
    2023-10-04:用go语言,现有一棵无向、无根的树,树中有n个节点,按从0到n-1编号给你一个整数n和一个长度为n-1的二维整数数组edges,其中edges[i]=[ai,bi]表示树中节点ai和bi之间存在一条边。每个节点都关联一个价格。给你一个整数数组price,其中price[i]是第i......
  • 2023-10-04:用go语言,现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你
    2023-10-04:用go语言,现有一棵无向、无根的树,树中有n个节点,按从0到n-1编号给你一个整数n和一个长度为n-1的二维整数数组edges,其中edges[i]=[ai,bi]表示树中节点ai和bi之间存在一条边。每个节点都关联一个价格。给你一个整数数组price,其中price[i]是......
  • flask请求钩子(就是django的中间件)
    flask中的请求钩子就是域django的中间件类似,作用都是用于在请求前、后、响应前、后进行一些hook操作。请求钩子装饰器@app.before_request#请求前会调用,一般可以用来做权限校验。@app.brefore_first_request#只在第一次请求的时候调用,可以做一些init初始化的动作。......
  • flask蓝图(这玩意就是django的子应用)
    蓝图的概念类似django的子应用,作用就是分模块开发,有关联的都放在一起。蓝图的创建步骤:新建一个包(一个包就是一个模块、等同于一个子应用)在包的__init__.py中创建蓝图对象。蓝图对象所有的参数和功能与Flask()对象类似。见:user下的__init__.py和views.py在app中注册蓝......
  • go定时任务
    目录go定时任务TimeNewTimerNewTickercron文档go定时任务TimeNewTimerpackagemainimport( "fmt" "time")funccronTimer(){ /** timer定时器实现一些定时操作本质通过chan阻塞实现且只执行一次 t:=time.NewTimer(时间):新建定时器 <-t.C:管道取值阻塞 t.......
  • django集成celery
    参考:https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#django-first-steps这里只记录一些要注意的地方1、celery主文件importosfromceleryimportCelery#SetthedefaultDjangosettingsmoduleforthe'celery'program.#这个是导入djan......
  • django-celery-results - 使用 Django ORM/Cache 作为结果后端
    https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#django-celery-results-using-the-django-orm-cache-as-a-result-backend这个一般自己设置一下result_backend也行,要用django-celery-results也是一个选择。......