Skip to content


Notes on Go Structure Tags

Go has a feature to create a user-defined type with struct. Go also allows the program to specify meta-information with structure field.
This meta-information is called Tags. A tag usually helps in packaging/unpacking a user-defined type structure.

  package main

  import (

  type Test struct {
      // json tags indicate the key name to use in json data
      Name    string `json:"myname"`
      Country string `json:"region"`

  func main() {
      // The order of json need not match the order of structure fields.
      p := map[string]string{"region": "earth", "myname": "hello"}

      marshalled, err := json.Marshal(p)
      fmt.Println(marshalled, err)

      // get the var back from marshalled data
      var m Test
      json.Unmarshal(marshalled, &m)
      fmt.Printf("name=%v\n", m.Name)
      fmt.Printf("name=%v\n", m.Country)

Why should I use json tags

  • The benefit of json tags is the flexibility to marshal with either a JSON or a struct.
  • JSON input for marshaling is convenient and flexible with elements ordering.


Notes on String & Encoding Techniques

String and their encoding decide the languages the code can support.


  • We have many languages and their symbols that need more than 8-bits (ASCII) for binary representation.
  • Encoding adds semantics to a set of bytes.
  • Unicode is a table of all characters and their numeric equivalent.
  • Since there are more than 100k symbols, 8-bits are not enough.

What is UTF-8

  • Unicode representation of a string needs 12-bits, padded to 32-bit.
  • The idea of UTF-8 is a variable encoding of symbols. The common ASCII symbols can use just 8-bits and other extended symbols can use up 1,2,3 or 4-bytes.
  • UTF-8 is more memory efficient compared to UTF-16 which uses minimum two bytes for a symbol.

How do Programs Use an Encoding?

  • The program picks a library for string encoding.
  • UTF is the most common library used.
  • The number of bytes in encoded string is decided by number of leading 1s in the array.


Unicode= \U+FFE8\U+FFA7/\U+FFE8\U+FFA6


Written with StackEdit.

The most efficient Image format- WebP?

The most efficient Image format- WebP?

I always knew that PNG followed by JPEG were the most efficnent, lossy image compression format. However, there is a new image format, webP. It is developed by Google and comes in lossy as well as lossless format.

How is it better than PNG/JPEG?

  • The webP image is ~30% smaller in storage size than its counterparts.
  • It is supported by all major browsers.

Why is it better?

  • It uses a borrowed techniques of vide compression, VP8.
  • VP8 is a preditive lossy compression technique for intra & inter frames.
  • webP uses intra-frame compression technique as we are dealing with images.
  • A layman explanation of VP8 is that it will divide the image in a nxn matrix, each cell is called a macroblock.
  • A macroblock is checked for motion. Some blocks have no motion, such as sky. These are called ‘key’ frames.
  • A key frame checks its predicted frames and encoded as a diff.
  • The diff is quantized and encoded using a better encoding (Arithmatic Encoding) than Huffman coding used in JPEG.

Should you use webP?

  • Yes, it is well supported, always better compressed than PNG/JPEG.
  • Uses slightly more RAM to encode than PNG


Written with StackEdit.

Why use Base64 Encoding?

What is Base64 encoding?

  • Given a stream of binary bits, it will encode 6-bits to a character from a set of 2 pow 6 (64 chracters).
  • Example “abcd”, the ASCII representation is 65666768.
  • [1000001][1000010][1000011][1000100]
  • Base64 would pics six continuous bits
  • 100000|| 110000|| 101000|| 011100||0100xx here xx would be 00 (padding)
  • gwocQ

Why use base64 encoding?

  • Transferring binary data in URLs
  • Tranferring binary data such as images as text
  • Transmit and store text that might cause delimiter collision.
    • Example is a random string followed by a delimiter (_) and a pattern and the code logic searches the delimiter to seperate the pattern.
    • The _ can appear in the generated random string too.
    • So encoding the random string in base64 would avoid such case.
    • Embed image in a XML
%d bloggers like this: