Go Quiz: Test your knowledge of the programming language
Have you mastered Golang? Why not test your knowledge by taking this quiz! In it, we’ll be investigating interesting aspects and corner cases of Golang. While the answers to some questions will appear obvious, others will require you to take a closer look — even for the most experienced Go developers. By the end of this article, we hope you’ll have gained a deeper understanding of the programming language and its underlying philosophy.
So without further ado, let’s get started!
Value assignment
1. What value y
will have at the end of the execution?
According to the specification, for
loop creates its own scope. Therefore, we are dealing with two different scopes there: one insidemain
function, and one inside the for
loop. Therefore, we don't reassign y
inside the for
loop initialization, but instead creating a new y
that shadows the one from the outer scope. Therefore, outer y
is not affected, and the program will output 0
.
A part of a string
In this example, we have a string and would like to access a part of it.
2. What would be the result of the following snippet?
The first two print statement would output the same result. A string in Golang is an immutable array of bytes and every character is encoded in UTF-8. In this case, we are dealing with the ASCII-only string, therefore, character 9
will be encoded as a single byte with a value equal to 57
. Therefore, the first print statement would output 57
. Exactly the same value would be printed at the second line, as in this case, we will have rune r
that consists of a single byte.
However, the program won’t compile due to the third line, as we are dealing with different types: uint8 (under alias byte
) and int32 (under alias rune
). The numeric value of the variables is equal, but their types are different, therefore, they cannot be compared without the explicit type conversion.
Struct Conversion
In this example, we have two similar structs that differ only in struct tags. Such an approach could be used in a real life. For example, you can have a separate representation of a single domain model in different packages: package db
that is responsible for database persistence and package api
that is responsible for handling the incoming requests. In this case, the structs would be equal save for the struct tags.
3. What would be the result of the following code snippet?
#v
outputs the full Golang representation of the value, including the type of the struct and its field names.
That’s a tricky question because according to the Golang specification a struct tag is a part of the struct definition. Therefore, at some point, it wasn’t possible to do the conversion. However, later the Go team decided to relax the constraint (without changing the definition of the struct in the spec), and now such conversion is permitted.
main.Struct2{A:1}
4. How about this snippet? We are trying to convert Struct1
to Struct2
. All information necessary for Struct2
is available in Struct1
. However, there is also a redundant field B
in Struct1
.
In this case, the specification does not care whether we have all the information to instantiate Struct2
from Struct1
. Struct1
has an extra field, and that's the end of the deal: the operation is not permitted, and the code won't compile.
JSON Unmarshalling
5. Will the existing records in the map be preserved when we unmarshal JSON-encoded values into it? What happens in the case of a collision (note key Field1
)?
Existing records in the map will be preserved. In the case of a collision, the value will be overwritten.
map[Field1:1 Field2:202]
6. What about structs?
The same logic is valid here:
{Field1:1 Field2:202}
And that all the question for today. How many right answers did you get out of six?
From my personal point of view, one of the main advantages of working is SumUp is the abundance of opportunities to Grow. As a developer in SumUp, I can use my own personal budget, spend every other Friday as a hack day and participate in various engineering discussions inside the whole company. The article above was initially presented as a quiz in one of the regular internal meetings — Engineering Sync of my tribe.
If you’re interested in working as a Golang Engineer, we have an open position (and some more engineering positions here). Prior knowledge of Golang is not required. Keep in mind, I’m not a recruiter, but I can share my personal experience as a Backend Engineer working at SumUp.