Let’s start with quiz comparing two programs which differ only by single statement. Program C uses constant declaration (source code):

and program V uses variable declaration (source code):

Before scrolling down to see answers, please think for a moment about results of both C and V

.

.

.

.

.

.

.

Program C writes to stdout 1. The second one (V) might be…


One more gotcha with re-using range clause variables.

This story will start with a puzzle (source code):

Think for a moment about what will be written to stdout? The answer is not that simple. It can be actually many different things. Program will always print foo, bar and baz, each in separate line but the order might be different each time:

It’s really up to the scheduler how…


Tale of comparison operators and {DeepEqual,Equal,EqualFold} methods

You’ve probably seen == or != operators many times in .go files. It turns out that some types like e.g. maps or slices can’t be used as operands to these operators. It’s sometimes needed to compare such values though. In this post we’ll explore all rules behind equality operators in Go, how it plays with Unicode and what are the methods to compare non-comparable types.

Let’s start with something simple and actually not hiding many secrets. For booleans and numeric types (floats, integers, complex numbers) equality operators work without bigger surprises. Type float64 has few special values like NaN (IEEE…


Deferred function is declared inside another function and is called when surrounding function exits — either normally or if goroutine panics. This mechanism is used e.g. to do cleanup and close open files or sockets.

Deferred functions are evaluated in LIFO fashion (source code):

Output:

defer statement can be used anywhere inside the function so it’s not limited only to its beginning (source code):


In contrast to many other languages, Go doesn’t have multiple constructs for executing block of code repeatedly. There’s single statement (for statement) which takes different forms to support various scenarios and also integrates well with Go-specific mechanisms like slices or channels. In this post we’ll examine under a microscope something which is more interesting than it may seem at first glance.

1. Single boolean condition

Let’s start with the simplest form where only optional bool condition is in use (source code):


(source code)

Think for a moment about expected output of this program..

For some (including me) the result might be surprising at first glance:

I’ve personally expected

but it turned out to be wrong answer.

Iteration variable t is declared using short variable…


Solving readers-writers problems

Readers-writers problems (plural since there’re some variations) occur when shared piece of data needs to be accessed by multiple threads. There’re two types of threads accessing data — readers and writers. Readers only read data and writers modify it. When writer has access to data then none other thread (reader or writer) can share such access. This constraint takes place in real life when e.g writer cannot modify data (like database) in atomic way so reading incomplete changes must be blocked to prevent loading corrupted data. There’re many modifications of the core problem like:

  • writers cannot be starved (waiting indefinitely…


how to use channels to write idiomatic code in Go

Problem

Let’s suppose we’re running an IT company employing programmers and testers. To give people a chance to get to know each other and relax a bit we’ve bought a ping-pong table and established following rules:

  • exactly two people can play at the same time,
  • next pair can start their game only when previous one finished so it’s not allowed to switch one player,
  • testers can only work with programmers and vice versa (never two testers or two programmers together). If programmer or tester wants play then needs to wait for tester or programmer respectively to establish a valid pair.


How to implement them using buffered channels

In this story we’ll examine basic synchronization problem to see how concurrency primitives in Golang like buffered channels can be used to implement concise solutions.

Problem

Let’s suppose we’ve a number of workers. To take advantage of multiple CPU core every worker runs inside separate goroutine:

Worker does a series of jobs:

Each job is preceded with bootstrap that needs to be synchronized across all workers — worker needs to wait for all other…


Couple of days ago Go 1.10 has been released — six months after 1.9. New version brings bigger and smaller changes (release notes) but I would like to discuss one related to net/http package. Version 1.10 supports proxies talking over HTTPS (commit). Previously it was possible to communicate only with proxies using plain (unencrypted) HTTP. Let’s create an environment to confirm it actually works.

Server

To test this change please launch simple HTTP(S) proxy server written in Golang:

Client

Michał Łowicki

Software engineer at Datadog, previously at Facebook and Opera, never satisfied.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store