boot2docker: Add volume from windows host to the docker container

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 virtual box 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 exported 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 example execute 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 example execute the following command:

go run cgo2.go

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

//export func-name

All the exported Go functions can be accessed from C by including the auto-generated header file _cgo_export.h.


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

go build cgo3
./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.

Polymorphism

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
main.main()
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.