Pack 1 Flashcards

1
Q

How do you handle concurrent reads and writes to a map in Go?

A

Go’s maps are not thread-safe. Use sync.Mutex or sync.RWMutex for manual locking, or use sync.Map for a built-in thread-safe map implementation.

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

What is sync.Map in Go?

A

A sync.Map is a concurrent map provided by Go’s standard library. It is designed for safe concurrent use by multiple Goroutines without additional locking.

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

How does sync.Map differ from a regular map?

A

sync.Map includes built-in synchronization mechanisms, so you don’t need to use explicit locks. It also provides methods like Load, Store, Delete, and Range for common operations.

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

What is the purpose of sync.RWMutex in Go?

A

sync.RWMutex is used for fine-grained locking, allowing multiple Goroutines to read simultaneously but only one Goroutine to write at a time.

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

How do you use a sync.Mutex in Go?

A

To use sync.Mutex, you lock it before accessing shared data and unlock it afterward.

var mu sync.Mutex  
mu.Lock()  
// Access shared resource  
mu.Unlock()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between sync.Mutex and sync.RWMutex?

A

sync.Mutex allows only one Goroutine to access a critical section, while sync.RWMutex lets multiple Goroutines read simultaneously but restricts write access to one Goroutine at a time.

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

How do you iterate over a sync.Map in Go?

A

Use the Range method to iterate over all key-value pairs in a sync.Map.

var sm sync.Map  
sm.Store("key1", "value1")  
sm.Range(func(key, value interface{}) bool {  
    fmt.Println(key, value)  
    return true // continue iteration  
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the context package used for in Go?

A

The context package is used for managing deadlines, cancellation signals, and carrying request-scoped data across API boundaries.

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

How do you create a context with a timeout in Go?

A

Use context.WithTimeout.

ctx, cancel := context.WithTimeout(context.Background(), time.Second)  
defer cancel()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you detect deadlocks in Go?

A

Deadlocks occur when Goroutines wait indefinitely for resources. They can be detected using Go’s built-in race detector by running your code with go run -race or go test -race.

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

What is the difference between buffered and unbuffered channels in Go?

A

Buffered channels have a capacity and do not block the sender until the buffer is full. Unbuffered channels block the sender until the receiver is ready.

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

and why would you do it?How do you close a channel in Go

A

Use the close function to close a channel. Closing a channel signals that no more values will be sent, allowing receivers to terminate gracefully.

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

What happens when you try to send to a closed channel in Go?

A

Sending to a closed channel causes a panic.

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

What is the purpose of Go’s runtime.Gosched() function?

A

runtime.Gosched() yields the processor, allowing other Goroutines to run. It does not block the current Goroutine.

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

What is the purpose of the sync.Map in Go?

A

sync.Map is a concurrent map that is safe for use by multiple Goroutines. It provides built-in synchronization for read and write operations.

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

How do you use sync.Map in Go?

A

```
var m sync.Map
m.Store(“key”, “value”)
value, ok := m.Load(“key”)
m.Delete(“key”)
~~~

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

What is the purpose of the context package in Go?

A

The context package is used to carry deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.

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

How do you create a context with a timeout in Go?

A

```
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
~~~

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

What is the purpose of the context.WithCancel function in Go?

A

context.WithCancel returns a copy of the parent context that is canceled when the returned cancel function is called.

20
Q

How do you use context.WithCancel in Go?

A

```
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
~~~

21
Q

What is the purpose of the context.WithValue function in Go?

A

context.WithValue returns a copy of the parent context that includes the provided key-value pair.

22
Q

How do you use context.WithValue in Go?

A

```
ctx := context.WithValue(context.Background(), “key”, “value”)
~~~

23
Q

What is the purpose of the context.Background function in Go?

A

context.Background returns a non-nil, empty context. It is the top-level context for incoming requests.

24
Q

What is the purpose of the context.TODO function in Go?

A

context.TODO is used when it’s unclear which context to use or if the function has not been updated to accept a context.

25
Q

How do you handle file I/O in Go?

A

Using the os package for basic file operations and the io package for reading and writing data.

26
Q

How do you open a file in Go?

A

```
file, err := os.Open(“file.txt”)
if err != nil {
log.Fatal(err)
}
defer file.Close()
~~~

27
Q

What is the purpose of the bufio package in Go?

A

The bufio package implements buffered I/O. It provides buffered readers and writers that improve performance for I/O operations.

28
Q

How do you use the bufio package to read a file in Go?

A

```
file, err := os.Open(“file.txt”)
if err != nil {
log.Fatal(err)
}
defer file.Close()

reader := bufio.NewReader(file)
line, err := reader.ReadString(‘n’)
~~~

29
Q

What is the purpose of the encoding/json package in Go?

A

The encoding/json package provides support for encoding and decoding JSON data.

30
Q

How do you encode a struct to JSON in Go?

A

```
type Person struct {
Name string
Age int
}
p := Person{Name: “John”, Age: 30}
jsonData, err := json.Marshal(p)
~~~

31
Q

How do you decode JSON to a struct in Go?

A

```
var p Person
err := json.Unmarshal(jsonData, &p)
~~~

32
Q

What is the purpose of the net/http package in Go?

A

The net/http package provides HTTP client and server implementations.

33
Q

How do you create a simple HTTP server in Go?

A

```
http.HandleFunc(“/”, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, “Hello, world!”)
})
http.ListenAndServe(“:8080”, nil)
~~~

34
Q

How do you make an HTTP GET request in Go?

A

```
resp, err := http.Get(“http://example.com”)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
~~~

35
Q

What is the purpose of the testing package in Go?

A

The testing package provides support for writing and running tests.

36
Q

How do you write a simple test in Go?

A

```
func TestAdd(t *testing.T) {
result := add(2, 3)
if result != 5 {
t.Errorf(“Expected 5, got %d”, result)
}
}
~~~

37
Q

What is the purpose of the go install command in Go?

A

The go install command compiles and installs Go packages.

38
Q

What is the purpose of the go get command in Go?

A

The go get command downloads and installs packages and their dependencies.

39
Q

What is the purpose of the go mod command in Go?

A

The go mod command provides modules support for managing dependencies.

40
Q

How do you initialize a new Go module?

A

By using the go mod init command.

41
Q

What is the purpose of the go vet command in Go?

A

The go vet command examines Go source code and reports suspicious constructs.

42
Q

What is the purpose of the go lint command in Go?

A

The go lint command checks Go source code for style and correctness issues.

43
Q

What is the purpose of the go fmt command in Go?

A

The go fmt command formats Go source code according to the standard style guidelines.

44
Q

What is the purpose of the go doc command in Go?

A

The go doc command provides documentation for Go packages and symbols.

45
Q

What is the purpose of the go generate command in Go?

A

The go generate command runs code generators specified by special comments in the source code.