Languages

Notes on Golang in one day

September 13, 2020 GoLang, Languages No comments

I’m a Software Engineer with some years of experience in C#, Java, JavaScript and tiny bit of other languages. For fun (I don’t yet need this at work) I spent one day going through GoLang tutorials, this video, and skimming through “The Go Programming Language” book. This blog post is a collection of things in Go that I found to be interesting and worth mentioning. In NO way this can be considered a tutorial or reliable source of information on the language. The best and most comprehensive place to learn about the language is here.

So there you go, golang in 20+ bullet-points:

  • There is shadowing for variables (package level variables can be overshadowed by local scope variables).
  • Package level variables can be exported just based on convention of whether they start with a capital.
  • complex type is a first class citizen type in golang much like int and string.
  • There is a type called “Rune” which is nothing more than Int32 alias to represent characters
  • iota is weird way of incrementing inside of const blocks resetting itself in a new constant block
  • arrays are completely copied on variable assignment b := a, if you want to reference same array you can do b := &a
  • slices are variable length reference arrays
  • there is spread syntax with three dots ... like in some other languages and/or frameworks
  • removing elements from a slice is a funny business requiring you to cut slice in two pieces not containing target element and then concatenating those pieces together
  • To define a map you write something like someMap := map[int]string{1: "one", 2: "two"} and then you get retrieve from it with indication if key exists getOne, ok := someMap[1]
  • You can break out of a parent loop from within nested loop by applying labels to loops.
  • There are no classes or inheritance as such. Closest things to this are struct and composition via process called embedding
  • Execution of code can be deferred. This is quite interesting, as executions is scheduled to the end of function block and if there are multiple defer statements they are unwind in FILO mode, even if there was exception thrown panic.
  • Yeah, there are no exceptions in golang, or better to say, they are not called exceptions but rather panic. Panic can be controlled by recover() function. Probably easiest way to think about this are throw and catch with a caveat that error thrown is not exactly same idiomatically as in other languages.
  • Go is similar to C/C++ as it has pointers, de-referencing, function parameters can be sent by value or reference. Go does not allow pointers arithmetic unless you are using unsafe.
  • Functions support named return values and multiple return values. This is really nice in my opinion.
  • Functions are a bit similar to functions in javascript from usage perspective. They can be immediately invoked and passed around as a param.
  • Interfaces can be applied on any type where you can add methods; they can be composed (just like structs); Go seems to be doing a lot of emphasis on interface segregation.
  • Goroutines combined with channels is probably something most interesting and unique about the language.
  • Goroutine is a thread abstraction, sometimes called “green thread”. They are cheap to create and easy to control. They do not span OS level threads (unless they must, I guess). The way to asynchronously call something is just go getMeADrink()
  • Goroutines can be controlled using waitgroup and mutex. This is standard shared memory multithreading model on very high level similar to other languages.
  • Channels is a way to pass data between goroutines. It can either be a single type or a buffer of things. This is when multithreading can be implemented much differently from other languages. This is also called communicating sequential processes model of multithreading.

I hope this was useful to some of you. For me personally this day gave me some idea what the language is about and the syntax no longer looks alien. Next thing to do would probably be to spend one more day playing with goroutines/channels specifically and actually reading the book for more fun.

Till next time!


No comments


Erlang programming language

December 13, 2012 Erlang, Languages No comments

Erlang logoThe one I didn’t get. But don’t worry, I can “kill” myself and “spawn” another me to try once again.

Not sure if I have to repeat some of the definitions given to this language, because I doubt that my readers read only this blog and never heard about Erlang. But who would start searching for “Erlang” if you are already here, so to avoid this dilemma I just copied one of its definitions later on.

Some of my thoughts

Erlang is great and hard language at the same time. Its syntax easily looks somewhat alien to many developers, not saying about normal people, who are capable of reading only few human languages. On the other hand ideas behind Erlang are just astonishing. “Let it crash” concept works, because there is no shared state between processes, and if something crashes you just restart stuff, not worrying about overhead as processes are lightweight. And how does this sound to you: dynamic language in which you can change code on the fly? I don’t think you can allow for downtime when you are in a rocket. Not sure if NASA uses Erlang, but many telecom companies use it for sure, which resulted in birth of Open Telecom Platform (OTP) – a library for telecom applications.

Why Erlang? – Built to kick ass!

I’ve found following explanation very sensible:

Erlang was developed at Ericsson and was designed from the ground up for writing scalable, fault-tolerant, distributed, non-stop, soft-realtime applications. Everything in the language, runtime and libraries reflects that purpose, which makes Erlang the best platform for developing this kind of software.

Use Erlang if you want your application to:

  • handle very large number of concurrent activities
  • be easily distributable over a network of computers
  • be fault-tolerant to both software & hardware errors
  • scale with the number of machines on the network
  • be upgradable & reconfigurable without having to stop & restart
  • be responsive to users within certain strict timeframes
  • stay in continuous operation for many years

Please visit http://veldstra.org/whyerlang/ for more “why Erlang?”.

How many are there Erlang developers?

This or some similar question often is very interesting when its about some exotic or small language. Hard to answer, but I’ve found this really nice site: http://roberto-aloi.com/languagesintheworld/#. Try it out to figure out who is coding Erlang/Haskell/Scala/… in your country.

Sleeping Barber problem in Erlang

In my Haskell post I wrote “I’m too new to Haskell to talk about some advanced features and to write programs which are over 10 lines of code.” I can ensure that now I can write Erlang programs which are more than 10 lines of code, but I won’t be able to confidently talk about most of aspects of this language either.

I can write larger programs, because I wrote one. Pitfall here is that I don’t completely understand how it works. I believe this is due to the fact, that I’ve chosen wrong problem to solve with this language. Erland is not really build for stuff like “run this for 10 seconds”, but rather for stuff like “that’s your message, deal with it”. Though, I could easily be wrong.

I decided that my first Erlang program will be “sleeping barber” problem, which I’ve already done in Clojure (BTW, there is small bug, spot it!). Once again:

Problem called "sleeping barber." was created by Edsger Dijkstra in 1965. It has these characteristics:

  • A barber shop takes customers.
  • Customers arrive at random intervals, from 10 to 30 milliseconds.
  • The barber shop has three chairs in the waiting room.
  • The barber shop has one barber and one barber chair.
  • When the barber’s chair is empty, a customer sits in the chair, wakes up the barber, and gets a haircut.
  • If the chairs are occupied, all new customers will turn away.
  • Haircuts take 20 milliseconds.
  • After a customer receives a haircut, he gets up and leaves.

Write a multithreaded program to determine how many haircuts a barber can give in 10 seconds.

Mine solution in Erlang

Counter – does nothing smart except of counting number of customers with new haircut

-module(counter).
-export([loop/1,incr/1,get_count/1]).

loop(Count) ->                            
    receive                                   
        { incr } -> 
            loop(Count + 1);              
        { report, To } ->                     
            To ! { count, Count },            
            loop(Count)                           
end.                                      

incr(Counter) ->
    Counter ! { incr }.

get_count(Counter) ->    
    Counter ! { report, self() },
    receive
        { count, Count } -> Count
end.

Barber

-module(barber).
-import(counter,[incr/1]).
-export([loop/0, cuthair/3]).

loop() ->
	receive
	{Pid, Counter}->
		timer:sleep(20),
		Pid ! "haircut done!",
		counter:incr(Counter),
		loop()
end.

cuthair(To, Barber, Counter) ->
	% make barber work synchronously
	Barber ! { To, Counter },
    receive
        BarberService -> BarberService
end.

Barbershop

Crazy stuff. For example, take a look at how I implemented continuous generation of customers. It is tail recursion function, which takes 10000 as starting WorkTime and generates customer after 10-30ms of thread sleeping, then calls itself with WorkTime minus time needed to send customer to barbershop. It continues until function is out of WorkTime. Similar I do for barber_work_day.

-module(barbershop).
-export([loop/3,customers/2,barber_work_day/4]).

loop(FreeChairs, Barber, Counter) ->
receive
	{ customer } ->
		loop(FreeChairs-1, Barber, Counter);
	{ barber } ->
		loop(FreeChairs+1, Barber, Counter);
	{ free_chairs_count, To } ->
		To ! { chairs, FreeChairs },
		loop(FreeChairs, Barber, Counter)
end.                    

barber_work_day(WorkTime, Barbershop, Barber, Counter)
			when WorkTime < 1 -> 1;
barber_work_day(WorkTime, Barbershop, Barber, Counter)
			when WorkTime > 0 -> 
receive
{ start } ->
	self() ! { start },
	X = 20,
	FreeChairs = get_free_chairs_count(Barbershop),
	case FreeChairs of
	_ when (FreeChairs < 3) -> 
		timer:sleep(X),
		Barber ! { self(), Counter },
		Barbershop ! {barber},
		barber_work_day(WorkTime-20, Barbershop, Barber, Counter);
	_ ->
		barber_work_day(WorkTime, Barbershop, Barber, Counter)
	end
end.
	
	
get_free_chairs_count(Barbershop) ->    
    Barbershop ! { free_chairs_count, self() },
receive
        { chairs, FreeChairs } -> FreeChairs
end.

customers(WorkTime, Barbershop) when WorkTime < 0 -> 1;
customers(WorkTime, Barbershop) when WorkTime > 0 -> 
receive
{ start } ->
	self() ! { start },
	X = random:uniform(21) + 9,
	timer:sleep(X),
	
	FreeChairs = get_free_chairs_count(Barbershop),
	case FreeChairs of
	_ when (FreeChairs > 0) -> 
		Barbershop ! {customer},
		customers(WorkTime-X, Barbershop);
	_ ->
		customers(WorkTime-X, Barbershop)
	end
end.

Program

Here I just initialize random, create processes and kick-off generation of customers and barber’s work day.

{A1, A2, A3} = now().

random:seed(A1, A2, A3).

Counter = spawn(fun() -> counter:loop(0) end).

Barber = spawn(fun barber:loop/0).

Barbershop = spawn(fun() -> barbershop:loop(3,Barber,Counter) end).

Customers = spawn(fun() ->
	barbershop:customers(10000,Barbershop) end).

BarberWorkDay = spawn(fun() -> 
	barbershop:barber_work_day(10000,Barbershop,Barber,Counter) end).

BarberWorkDay ! {start}.

Customers ! {start}.

counter:get_count(Counter).

Un-obviously it executes slightly longer than 10 seconds (12-15), so when I call counter:get_count(Counter) exactly after 10 seconds I’m getting slightly lower numbers than expected, so I just call get_count in console until number stops growing with some very realistic number, like 434.

Now you can blame me for my dumbness, because it is really a lot of code for quite simple problem and it took me good 6+ hours to implement.

Links:

I really hope you liked this post! If true, instead of blaming me, you are welcome to share your own experience with Erlang.


No comments


Clojure programming language

November 30, 2012 Clojure, Languages 2 comments