Golang - Geek went Freak!


Golang: Channel send behavior

General way to send data to a channel in golang is

lChan := make(chan bool)
lChan <- true

It is important to understand that, golang channels block on send until there is either

  1. Space left to buffer the data
  2. Until the data is read from the channel, if there is no buffer space left

Non-buffered channels (default)

This means that, if there is no one reading the data from the channel, the code blocks.

Here is an example:

When the program is run, golang automatically detects the deadlock during runtime and report it:

Note: When buffer size is 0, the channel blocks until the data is read from the channel. This was the behavior witnessed in first example.

This fate can be postponed by increasing the buffer size of the channel. By default, the buffer size is 0.

Buffered channels

Optional second argument to make function takes length of channel buffer as input and returns a new channel with the requested buffer size. It is not possible to change the size of the channel buffer after it is created.

package main

import (

func main() {
	lChan := make(chan bool, 5)
	lChan <- true


This program doesn’t block. But eventually it will when the buffer is filled up. Consider this example where buffer length is 5, we are trying to send 10 values to the channel but only reading 3. This should deadlock when we try to send 9th value.

When the program is run, golang automatically detects the deadlock during runtime and report it:

And it does!

Nonblocking send

One can use non-blocking send using the select statement. select statement is generally used to simultaneously send/receive to/from multiple channels.

When select has a default clause, the select statement will not block during send/receive.


Nonblocking send with timeout

Nonblocking sends are great. But won’t be nicer if we waited a bit before giving up? time package has a function called After. This function takes Duration as time.Duration type and returns a bool channel that is sent a message after the duration expires. After function can be used in select statement, achieve timeout functionality.


Golang: Cast array/slice of a type to array/slice of interface

Suppose you have this struct:

type (
    Type struct {
        X int

You want to convert this to an interface:

type (
    Interface interface {

You would do that in go in the following way:

t := Type{5}
var i Interface
i = t

Similarly, you would expect conversion of array/slice of Type to array/slice of Interface to work:

tlist := []Type{Type{0}, Type{1}}
var ilist []Interface
ilist = tlist

That would produce the following error:

cannot use tlist (type []Type) as type []Interface in assignment

Opps. So what happened?

The reason why Golang could not convert array/slice of a type to array/slice of an interface is clearly mentioned in the official FAQ. But this fact is very easy to miss if you are new to Golang.

Add volume to boot2docker

boot2docker is great. It allows us to develop using docker even on Windows PCs. One feature that is totally awesome is its ability to share folders on windows host with boot2docker virtualbox image. This shared folder from windows can then be used as a volume in the docker containers.

The first part is available to you out of the box. boot2docker shares the c:\User directory with the boot2docker virtualbox image and mounts it under /c/. Yay!

You can add this folder or part of it to the docker container as you would with normal folders.

docker run -it -v /c/:/data/ : bash

This will add the whole C:\User under /data in the launched docker container.

Golang CGo: Passing and receiving strings with C

In this post, we will see how to pass strings to a C function and receive strings returned from a C function. CGo provides convenient functions C.CString and C.GoString to convert Golang string into C char * and vice versa.

It should be noted that C.CString returns a dynamically allocated char array and should be freed to avoid memory leaks.

If go compiler throws an error like this:

error: ‘free’ undeclared (first use in this function)

It is because you are missing stdlib.h include which provides the free function. Note: stdlib.h should be included in Go file.

Golang: Call C code from Go code

Be it a library written in C or parts of your implementation you want to implement in C, it is very easy to call them from Golang.
All C functions, types, convenience function, etc are e,ported through C package in Golang. The C code can be directly embedded in Go code by writing it as comments directly above the statement that imports C package.

To run the above e,ample e,ecute the following command:

go run cgo1.go

You can also write C code in a separate .c file and access them in Go.

To run the above e,ample e,ecute the following command:

go run cgo2.go

To call a Go function in C code, e,port the Go function by writing the following snippet directly above the Go function:

//e,port func-name

All the e,ported Go functions can be accessed from C by including the auto-generated header file _cgo_e,port.h.

It should noted that this e,ample won’t work unless you place the project under $GOPATH/src and e,ecute go build on the project. For e,ample move the two files to $GOPATH/src/cgo3 and e,ecute the following commands:

go build cgo3

Golang: io.Reader to String or byte array

Request.Body in http package is an instance of io.ReadCloser, which in turn is an instance of io.Reader. Processing body of the request as io.Reader has several advantages. it is memory efficient. But there are times when we want it as string rather than stream. This short snippet converts io.Reader stream into string:

GoLang: Inheritance by embedding

Inheritance in Go

In Go, inheritance is emulated by embedding anonymous member of parent struct in the derived struct. Lets say, we have a Mammal and a Fish struct and we want to build a Amphibian struct from these two structs. In Go, you would do it like this:

By embedding anonymous members in a struct, you can access the members of the embedded struct using dot notation directly on the embedding struct.


In OOP languages, inheritance also allows polymorphism, through which instance of a derived class can be assigned to the instance of a parent class. Unfortunately Go doesn’t allow this:

The above code produces following error: > invalid type assertion: salamander.(Mammal) (non-interface type Amphibian on left)

The embedded anonymous member can be accessed using the syntax EmbeddingInstance.EmbeddedStruct syntax. This can also be assigned to an instance of embedded struct type.

Ambiguity due to multiple inheritance

When multiple anonymous members are declared in a struct and they have same members, Go cannot uniquely identify which of the members you are referring to. This creates ambiguity:

ambiguous selector salamander.Lips

This ambiguity can be resolved by using full path to the ambiguous member.

Embedding anonymous pointer members

Anonymous pointer to struct can also be embedded into a struct. Anonymous pointer members enable the same kind of inheritance normal embedded non-pointer members provide. But you should make sure you initialize the anonymous pointer member before using it. Failing to do so will give an error like this:

Salamander panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x2370]

goroutine 1 [running]:
runtime.panic(0x9aa80, 0x1660b9)
/usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
pointer_embedding.go:34 +0x370
exit status 2

Here is a working example with proper initialization of anonymous pointer members:

Right now, I am not aware of any advantages of using anonymous pointer members. If you know any, please share them in the comments.

GoLang: Marshall slice and map members of struct into JSON

Go has an incredibly user friendly built-in JSON encoder and decoder. One can create a struct with slice and map members and expect the encoding/json package to marshal and un-marshal it without any hassle. Here is a showcase program of how to marshal and unmarshal a struct containing slice and map members:


Before marshaling:  
IntF: 5  
StringF: 55  
FloatF: 55.500000  
SliceF: [5 55 555]  
MapF: map[5:%!s(int=5) 55:%!s(int=55) 555:%!s(int=555)]  

Marshalled data: {"IntF":5,"StringF":"55","FloatF":55.5,"SliceF":["5","55","555"],"MapF":{"5":5,"55":55,"555":555}}

Unmarshaled data:

After unmarshaling
IntF: 555
StringF: 5
FloatF: 5.555000
SliceF: [555 55 5]
MapF: map[5.5:%!s(int=0) 55.5:%!s(int=0) 555.5:%!s(int=0)]