Golang - Geek went Freak!

Golang

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
fmt.Println(i)

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.

C: Initialize multiple variables in same declaration statement

First thing first, never initialize nor declare multiple variables on the same line. It makes things very difficult to understand. But if you have to, this post will help you.

Initializing multiple variables in same line like this:

int p, q = 10;

is ambiguous because it is hard to understand if only q is initialized to 10 or if p is also initialized to 10. Of course, in this case only q is initialized to 10. To avoid the ambiguity, each initialization can be done in its own line.

int p;
int q = 10;

Also, try to initialize automatic variables (especially pointer variables) when they are declared.

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
./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.