{"id":1299,"date":"2021-01-30T12:43:47","date_gmt":"2021-01-30T12:43:47","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1299"},"modified":"2021-01-30T12:43:49","modified_gmt":"2021-01-30T12:43:49","slug":"golang-encoding","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/01\/30\/golang-encoding\/","title":{"rendered":"GoLang: encoding"},"content":{"rendered":"<p>Package encoding provide capabilities to convert data to and from byte-level and textual representations.<\/p>\n<ul>\n<li>encoding\/gob<\/li>\n<li>encoding\/json, and<\/li>\n<li>encoding\/xml<\/li>\n<\/ul>\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>The\u00a0<em>gob<\/em>\u00a0is Go\u2019s format for\u00a0<em>serializing<\/em>\u00a0and\u00a0<em>deserializing<\/em>. The gob was tailored for communicating between two servers written in Go.<\/p><\/blockquote><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Only exported fields are encoded; zero values are not taken into account<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Package gob manages streams of gobs &#8211; binary values exchanged between an Encoder (transmitter) and a Decoder (receiver).<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Format GoB<\/h2>\n\n\n\n<p>Let&#8217;s create a simple Type <code>Blog<\/code> and we will encode and decode it using the gob format.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/golang.org\/pkg\/encoding\/gob\/#NewEncoder<\/li><li>https:\/\/golang.org\/pkg\/encoding\/gob\/#Encoder.Encode<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>package enc\n\nimport (\n\t\"encoding\/gob\"\n\t\"flag\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n)\n\n\/\/ Blog blog structure\ntype Blog struct {\n\tTitle, Author, Category string\n\tLength                  int\n}\n\nfunc (b *Blog) String() string {\n\treturn fmt.Sprintf(\" Blog: %s @ %s\\n Category: %s\\n Length: %d\", b.Title, b.Author, b.Category, b.Length)\n}\n\nvar fileName string\nvar logger = log.New(os.Stdout, \" ENC: \", log.LUTC)\n\nfunc init() {\n\tflag.StringVar(&amp;fileName, \"f\", \"\", \"Output\/Input file\")\n\tflag.Parse()\n}\n\nconst (\n\t\/\/ErrorMessage For the errors\n\tErrorMessage = \"need a file to write or read the contents.\"\n)\n\nvar defBlog = Blog{Title: \"My first blog\", Author: \"Saurabh Sharma\", Category: \"technical\", Length: 200}\n\n\/\/ Cmd main entry method\nfunc Cmd() {\n\tif flag.NFlag() == 0 {\n\t\tflag.PrintDefaults()\n\t\tlogger.Fatalf(\"%s\", ErrorMessage)\n\t}\n\n\tf, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)\n\n\tdefer f.Close()\n\n\tif err != nil {\n\t\tlogger.Fatalf(\"%s\", err.Error())\n\t\tos.Exit(1)\n\t}\n\n\tenc := gob.NewEncoder(f)\n\terr = enc.Encode(&amp;defBlog)\n\tif err != nil {\n\t\tlogger.Fatalf(\"%s\", err.Error())\n\t\tos.Exit(1)\n\t}\n\n\tdec := gob.NewDecoder(f)\n\tvar blogR Blog\n\n\tf.Seek(0,0)\n\terr = dec.Decode(&amp;blogR)\n\tif err != nil {\n\t\tlogger.Fatalf(\"%s\", err.Error())\n\t\tos.Exit(1)\n\t}\n\n\tfmt.Printf(\" Read: %s\\n\", blogR.String())\n\tfmt.Println(\" --- End Program -- \")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main -f abc.txt\n Read:  Blog: My first blog @ Saurabh Sharma\n Category: technical\n Length: 200\n --- End Program -- <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Package encoding provide capabilities to convert data to and from byte-level and textual representations. encoding\/gob encoding\/json, and encoding\/xml The\u00a0gob\u00a0is Go\u2019s format for\u00a0serializing\u00a0and\u00a0deserializing. The gob was tailored for communicating between two servers written in Go. Only exported fields are encoded; zero values are not taken into account Package gob manages streams of gobs &#8211; binary values [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1304,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137,34],"tags":[153,154,23],"class_list":["post-1299","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-encoding","tag-gob","tag-golang","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1299"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1304"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}