Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 GO


  In-memory key-value store in C, Go and Python

Subtitle: Wow Go’s net library is fastOn paternity leave for my second child, I found myself writing an in-memory hashmap (a poor-man’s memcached), in Go, Python and C. I was wondering how hard it would be to replace memcached, if we wanted to do something unusual with our key-value store. I also wanted to compare the languages, and, well, I get bored easily!The code is on github as Key-Value-Polyglot.Each version implements enough of the get and set commands from the memcached protocol that we can test them with a memcached client.If you write a version in a different language (...

4,445 0       MEMORY C PYTHON GO KEY-VALUE


  Introduction to GoLang generics and advanced usage

Generics in Go allow you to write code that can work with multiple types of data, without having to write separate versions of the code for each type. This can make your code more flexible and easier to maintain, as you only need to write and test the code once, rather than maintaining multiple versions.To use generics in Go, you first need to define a type parameter, which is a placeholder for the type that the code will work with. For example, you might define a type parameter called "T" like this:func MyFunction(x T) T { // code here}You can then use the type parameter "T" wherever you wou...

4,244 2       GOLANG GENERICS


  Go vs C benchmark. Could Go be faster than C?

During last semester I was attending Multiprocessor Architectures course, given at Facultad de Informática where I study my Computer Science degree.As part of the assignments due to pass the course, we had to do several programs written in C to benchmark matrix multiplication by testing different techniques and technologies. First of all we had to do a secuential program in three different versions:A normal one where the result matrix is ordered by rows and the loops range the matrix by rows tooAn “inter” version where the result matrix is ordered by rows but the loops range the...

4,120 0       COMPARISON C SPEED GP BENCHMARK FASTER


  How to do pprof for gRPC service

gRPC is a RPC framework based on HTTP and is frequently used for communications among micro service inside the same organization network. However,  the service functions cannot be accessed via normal HTTP URL as it's not a WEB framework. In this case, how to do pprof on a gRPC service?The trick is starting a HTTP server asynchronously while starting the gRPC service. This HTTP server can be accessed to run prrof debug.go func(){ http.ListenAndServe(":10001", nil)}()Since it uses the default ServerMux, the pprof related routes will be registered automatically after importing the package&nb...

3,938 0       PPROF GOLANG GRPC


  The Go Pointer Magic

Go is a language with the pointer type, by which we canPass pointer into a function and update value in-place.Add methods to a struct as (* T) A, which is different from (T) A().However, the pointer is type-safe in Go, meaning that there are such restrictions of the pointer.Different types of pointers are unconvertible.Pointer type cannot be used for calculation.Pointer types cannot be compared, either == nor !=.No mutual assignment between different pointer-type variables.For example, *int cannot be converted to *int32in the following code.func main() {...

3,935 0       GOLANG POINTER UNSAFE


  Behavior of defer function in named return function

In Go, there is a special concept of named return value in function where a returned value can have name. For example, below is a named function in Go.func returnNamed() (i int) { i = 1 return}When the function returns, the return value i will have a value of 1. Also, Go has a concept of defer which will execute a function just before the calling function exits. This is similar to what finally block does in other languages such as Java. For example, a defer function can befunc deferFunc() { defer func() { fmt.Println("In defer") }() fmt.Println("Start")}Both return and defer can change th...

3,880 0       DIFFERENCE DEFER GOLANG NAMED RETURN


  The internals of slice in GoLang

There are 3 components of slice:a) Pointer: Points to the start position of slice in the underlying array;b) length (type is int): the number of the valid elements of the slice;b) capacity (type is int): the total number of slots of the slice.Check the following code:package mainimport ( "fmt" "unsafe")func main() { var s1 []int fmt.Println(unsafe.Sizeof(s1))}The result is 24 on my 64-bit system (The pointer and int both occupy 8 bytes).In the next example, I will use gdb to poke the internals of slice. The code is like this:package mainimport "fmt"func main() { s1 := make([]int, 3, 5) ...

3,879 0       GOLANG SLICE


  Understand more about Go basics with one interview question

First, let's take a look at below Go interview question:package mainconst s = "Go101.org"// len(s) == 9// 1 << 9 == 512// 512 / 128 == 4var a byte = 1 << len(s) / 128var b byte = 1 << len(s[:]) / 128func main() { println(a, b)}What would be the output in your mind? The output would be 4 0. Surprising?Before getting to the output values, some concepts in Go need to be introduced and explained in more detail.len() len() is a built-in function in Go to get the length of array, slice or string. There is one important statement about len() in Go specification.For some argume...

3,870 0       LEN() SHIFT OPERATION CONSTANT GOLANG