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

Go - Encoding Data to gob Format Data

时间:2023-10-05 09:56:31浏览次数:37  
标签:err format data gob will file Go Data

Problem: You want to encode structs into binary gob format.


Solution: Use the encoding/gob package to encode the structs into bytes that can be stored or sent elsewhere.

 

The encoding/gob package is a Go library to encode and decode a binary format. The data can be anything but is particularly useful with Go structs. You should be aware that gob is a proprietary Go binary format. Although there are attempts to decode gob in other languages, it is not a widely used format like protobuf or Thrift. It is advisable to use a more commonly used format if you have more complex use cases for binary data.
Take a look at an example. You want to deploy small electricity meters all over a building to measure the consumption of energy in the building. The metering points are not only per floor but also per unit, per area, and even per meeting room. They will also be used in common areas, including lighting as well as larger loads like lifts and escalators. The information you gather will help you monitor any abnormal usage of electricity, identify wastage, and allocate costs to the different occupants of the building. You are going to deploy a lot of these meters all over the building so the communications use a low - powered wide area network (LP - WAN). Your requirement is for data packets to be small so they can be transported through the LP - WAN efficiently.
Start with a simple struct to capture the information from the meter:

type   Meter   struct   { 
      Id          uint32 
      Voltage     uint8 
      Current     uint8 
      Energy      uint32 
      Timestamp   uint64 
}

You set a unique identifier for each meter; the voltage, current, and energy are what is being measured; and the timestamp gives you the time the reading is taken. The voltage and current are measured at the moment, but the energy in kilowatt - hours is how much energy has been consumed since the meter started.

Next, see how you can take readings from the meter and write them to a stream to be sent across the LP - WAN. For this, you will assume the following meter - reading data will be available and construct a struct to contain the data:

var   reading   Meter   =   Meter { 
      Id :          123456 , 
      Voltage :     229.5 , 
      Current :     1.3 , 
      Energy :      4321 , 
      Timestamp :   uint64 ( time . Now (). UnixNano ()), 
}

You also use a file to represent the network and will write to it:

func   write ( data   interface {},   filename   string )   { 
      file ,   err   :=   os . Create ( "reading" ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  create  file:" ,   err ) 
      } 
      encoder   :=   gob . NewEncoder ( file ) 
      err   =   encoder . Encode ( data ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  encode  data  to  file:" ,   err ) 
      } 
}

First, you create a file named reading , which will be your Writer . You then create an encoder around this writer and call Encode on it, passing it the struct instance. This will encode the struct instance in the gob format and will write it to a file.

 

标签:err,format,data,gob,will,file,Go,Data
From: https://www.cnblogs.com/zhangzhihui/p/17743089.html

相关文章

  • C# Datagridview 标题/列内容完全居中及选中行突出显示
    一、列标题居中1.首先点击属性窗口的ColumnHeadersDefaultCellStyle属性进入属性设置子界面,并设置子界面属性Alignment的值为MiddleCenter(如下图)2.如果此时列标题未完全居中,那么找到列集合设置属性(Columns)进入子界面,将所有列头的SortMode属性设置为NotSortable(如下图)以上就是......
  • 工具 | 极其方便的谷歌翻译软件 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......
  • "Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/n
    docker-compose搭建elasticsearch出现问题例子如下version:'3'services:elasticsearch:image:elasticsearch:7.14.0container_name:elasticsearchenvironment:-"discovery.type=single-node"-"xpack.sec......
  • 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]是......
  • [Compose] Asynchronous Reactive Data with Promises
    Let’smakeusingtheobserversasynchronous!Thiswaywecanupdatethedataandhavemultipleobserversrunasynchronously.classAsyncData{constructor(initialData){this.data=initialData;this.subscribers=[];}//Subscribetochan......
  • Wpf经验技巧-使用 d:DataContext 指定 DataContext 的类型.
    VM代码:V代码(版本1):没有指定DataContext的类型,所以下面的绑定并不知道P1和P3到底是什么,也就无法在代码编辑时检测出绑定是否正确.如果写错了,只能等到程序运行并打开这个窗口时报错才能知道.V代码(版本2):通过d:DataContext指定了DataContext的类型,所以下面的绑定......
  • 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中注册蓝......