Golang Flashcards

1
Q

“for” loop over range clause provides possibility to initialize inline two loop variables. Which are these?

A
  • 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++
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which standard library is used for templating

A

text/template
html/template

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the interface{} mean?

A

It is an empty interface which is implemented by every value in the type system. Primitive types as well implement the empty interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Empty slice literal is?

A

Not needed :)

var s = []int

You do not have to initialize since variables are initialized ot their initial type value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Appending to slice

A

append(s S, x …T) S

It is a variadic function which means that we can append number of elements at once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the specification of:

Passing arguments to … parameters.

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which standard library contains basic synchronization primitives and which are basic ones?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which standard library contains functionality for measuring and displaying time?

A

import “time”

type Time - instant in time with nanosecond precision

type Duration - elapsed duration between two instants of time in int64 nanosecond precision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which standard library contains basic testing primitives?

A

import “testing”

func TestXxxx(t *testing.T) {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Type switches?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to attach an interface to a primitive type?

A

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)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Balanced BST package to use?

A

GoLLRB - Left-Leaning Red-Black BST

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

On which concurrency model is Go’s concurrency based?

A

It is based on the Hoare’s CSP - Communicating Sequential Processes which is based on channels and message passing via channels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define:

“go” statement.

A

“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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is goroutine?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Define:

Channels and channel types.

A

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
17
Q

Explain channel capacity and operations related to it.

A

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.

18
Q

Operations on channel values?

A

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
19
Q

Explain:

Pattern: Channels as a handle on a service?

A

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)
} }
20
Q

Channels as synchronization primitives.

A

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.

21
Q

Pattern: multiplexing channels.

A

If we create

22
Q

Pattern: restoring sequencing of goroutines (through channels).

A

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.

23
Q

Which library is used for basic string operations?

A

import “string”

Implements simple functions for string manipulation for searching, replacements, case changing ….

24
Q

What is a Go string?

A

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.
25
Q

String literal and UTF-8.

A

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.

26
Q

Define:

rune data type.

A

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.