Golang Flashcards
“for” loop over range clause provides possibility to initialize inline two loop variables. Which are these?
- index of the iteration
- value of the current set element
var result = 0 for **i, d := range digits** { if d \> 0 { if original % uint64(d) == 0 { result++ } } }
Which standard library is used for templating
text/template
html/template
What does the interface{} mean?
It is an empty interface which is implemented by every value in the type system. Primitive types as well implement the empty interface.
Empty slice literal is?
Not needed :)
var s = []int
You do not have to initialize since variables are initialized ot their initial type value
Appending to slice
append(s S, x …T) S
It is a variadic function which means that we can append number of elements at once.
Explain the specification of:
Passing arguments to … parameters.
For f as variadic function which has parameter p of type “…T” as last param
- If no argument is passed, the value of p is nil
- Otherwise, the value of p is slice of type []T with len() and cap() of size of the number of arguments passed.
Which standard library contains basic synchronization primitives and which are basic ones?
import “sync”
type Cond - synchronization point between many routines which might wait on a single condition until it broadcast the change.
type Mutex - standard mutual exclusion lock.
type Pool - pool of objects which is thread safe and can be used as a cross thread pool.
Which standard library contains functionality for measuring and displaying time?
import “time”
type Time - instant in time with nanosecond precision
type Duration - elapsed duration between two instants of time in int64 nanosecond precision.
Which standard library contains basic testing primitives?
import “testing”
func TestXxxx(t *testing.T) {}
Type switches?
Since golang has no generics, we have to make general code by accepting interface{} and then in the implementation of the function decide on the concrete function to run on arguments.
Type switch is a special construct to use as a helper in this situation.

How to attach an interface to a primitive type?
Point is to rename the primitive type into our own type and then implement certain interfaces around them. After that we can express in the signatures interface requirements which would work as well for primitive types.
type Int int
func (x Int) Less(than Item) bool { return x \< than.(Int) }
Balanced BST package to use?
GoLLRB - Left-Leaning Red-Black BST
On which concurrency model is Go’s concurrency based?
It is based on the Hoare’s CSP - Communicating Sequential Processes which is based on channels and message passing via channels.
Define:
“go” statement.
“go” statement starts execution of the provided function in a separate, concurrent thread of control named goroutine in the same address space as main process.
- when goroutine finishes, thread ends
- return values from the function are discarded
- when main() finishes it takes all the goroutines down with it.
go Server()
go func(ch chan<- bool) { for { sleep(10); ch <- true; }} (c)
What is goroutine?
- independent execution function, launched by the go statement.
- has its own stack, which grows and shrinks
- is not a thread, but they are multiplexed on top of threads so they run concurrently.
- can be seen as very cheap thread.
Define:
Channels and channel types.
Channel is a mechanism which provides goroutines means to communicate between eachother by sending and receiving values of a certain type.
- can be unidirectional or bidirectional
- sending ints: chan <- int
- receiving ints: <- chan int
- default bidirectional: chan int
- can be channel of channel
- chan<- <-chan int // same as chan<- (<-chan int)
- Created by make(chan int, 100) with buffer capacity of 100
Explain channel capacity and operations related to it.
Channels can be created with place for one or more items. When there is place only for 1 item, channel is blocking until both sender and client are ready to send/receive the item.
In buffered case, blocking happens when channel is full or empty.
nil channel is never ready to send or receive.
Operations on channel values?
Channels support several operations
- send statements
- ch <- 3 // send value 3 to channel ch
- send to nil blocks forever
- send to closed channel causes panic
- receive operations
- v1 := <-ch // pull out value
- receiving from nil blocks forever
- receiving on closed always succeds returning zero-value of channel item type
- x, ok = <-ch // checks if value was sent or it was reading from closed one
Explain:
Pattern: Channels as a handle on a service?
Common pattern is to wrap service with a thin wrapper which creates a channel, starts service and returns read-only channel.
- allows for easy use of multiple instances of the service
- keeps channel with the service together.
func main() { joe := **boring**("Joe") // returns channel ann := **boring**("Ann") // returns channel
for i := 0; i \< 5; i++ { fmt.Println(\<-joe) fmt.Println(\<-ann) } }
Channels as synchronization primitives.
Fact that channels are blocking on reading and writing as long as both sender and receiver are ready is a majore mean of synchronization.
For buffered channels, it gets more complicated regarding the synchronization.
Pattern: multiplexing channels.
If we create
Pattern: restoring sequencing of goroutines (through channels).
When goroutines execute, they do not run in order.
Sometimes we need to restore sequencing of their execution.
That is done so that goroutine returns a common synchronization channel, shared by all goroutines of same concurrent task, together with the message to the caller.
Caller then can coordinate to continue execution of the goroutines in a preferred order.
Which library is used for basic string operations?
import “string”
Implements simple functions for string manipulation for searching, replacements, case changing ….
What is a Go string?
String in Go is just a slice of bytes without any constraints on the content. The bytes are totally arbitrary.
- addressing s[i] will return the value of a particular byte, not generally character of the string.
String literal and UTF-8.
Go source code is always UTF-8 thus string literals get translated to UTF-8 if no byte level escapes are present.
Some people think Go strings are always UTF-8, but they are not: only string literals are UTF-8. As we showed in the previous section, string values can contain arbitrary bytes; as we showed in this one, string literals always contain UTF-8 text as long as they have no byte-level escapes.
Define:
rune data type.
The Go language defines the word rune as an alias for the type int32, so programs can be clear when an integer value represents a code point.
Moreover, what you might think of as a character constant is called a rune constant in Go. The type and value of the expression
‘⌘’
is rune with integer value 0x2318.