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.