Golang Type Assertions: Dynamic Type Checking

Type Assertions

Go supports dynamic type checking of variables. If the type of a variable is unknown till run-time, type assertions can run a validity check.

// getRequest definition
struct getRequest {
    name string
// Declare a void (anonymous) type variable
request interface{}

// Checking if variable request is not nil && 
// has type getRequest. 


  • Type Assertion does runtime type check of a variable.
  • It also checks that value is non-nil.
  • Return true on success, false otherwise.


Written with StackEdit.


Why Using Golang sync Pool is a Bad Idea?

Not an absolutely bad idea, but you need a careful understanding before using sync Pool.

Golang sync Pool is used to create a self-managed object pool, with just New/Get/Set functions. The Pool implementation uses a mutex based locking for thread-safe operations from multiple Go routines.

Design Principles
  • Golang sync. Pool use garbage collection to manage objects. The pool is essentially a list of user-defined blob objects that are GC’ed periodically.
  • The application uses Get/Put for using an object from the Pool.
  • The application defines a New method to create a new object.
  • The application does not bother about object memory management.
  • Unreferenced that is objects present in the Pool at a given time is GC’ed.
  • There is no finalizer for objects deletion.
  • Get randomly selected object from the list; if no object is available, creates a new object and returns it.
  • Put places an object back to the Pool.
  • The design wants to avoid hotspots and randomize object selections in Put and Get operations. So, Put and Get calls may not be co-related.
    • A Put op may randomly return without placing the object back to the Pool.
    • This intentional delay helps the Get to pick a new object.
What are Potential Problems with Pool
  • If the object has allocated resources (socket, buffer), there is no way to free them!
  • If the application has got an object and starts processing it with async operations, there is a risk of data corruption if the application returns the object to the Pool.
Best Use cases for Pool
  • Plain buffer management
  • Non-composite objects lifecycle management
Not to Use If
  • Objects that dynamically allocate resources (buffer, sockets)
  • Objects that are processed asynchronously.

Neovim, Vim and Go Setup on Mac

  • Installing neovim

       pip2 install --user neovim
       pip3 install --user neovim
  • If you need Homebrew’s Python 2.7 run
    brew install python@2

  • You can install Python packages with

    pip3 install <package>

    They will install into the site-package directory

  • Path of neovim rc

– So far the most complete Go-vim setup:

Written with StackEdit.

Golang Essentials for C & C++ Developers: Part III

The Empty Interface

  • An empty interface is used when type of a variable is unknown.
  • There are scenarios such as printf function, raw buffers where you would not know the type.

Use case

// Definition of a map is --> var map[key]value
// If we do not know the type of the value and want
// to use the map for generic types, then an empty
// interface helps

// scenario 1: map value type is unknown
	var mymap map[int] interface{}
	mymap = make(map[int]interface{})
	mymap[10] = "python"
	mymap[21] = 22
// scenario 2: map key and value types are unknown
	var mymap1 map[interface{}] interface{}
	mymap1 = make(map[interface{}]interface{})
	mymap1[10] = "python"
	mymap1[21] = 22
	mymap1["test"] = 10

An array with interface

var []arr interface{}{}

Written with StackEdit.