misc Flashcards

(81 cards)

1
Q

Front

A

Back

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

What is spf13/cobra used for?

A

It’s a Go library for building powerful CLI applications using commands, subcommands, and flags.

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

How do you define a new Cobra command?

A

Create a *cobra.Command with fields like Use, Short, Run, and optionally Long, Args, etc.

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

How do you add a subcommand in Cobra?

A

Use rootCmd.AddCommand(childCmd) where both are *cobra.Command objects.

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

What is Run in a Cobra command?

A

Run is a function that executes when the command is called. It’s usually of the form func(cmd *cobra.Command, args []string).

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

How do you parse command-line flags in Cobra?

A

Use cmd.Flags().String(...), Int(...), Bool(...), etc., to define flags and bind to variables.

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

How do you access parsed flag values in Cobra?

A

Use methods like cmd.Flags().GetString("flagname") inside your Run function.

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

How do you enforce required flags in Cobra?

A

Use cmd.MarkFlagRequired("flagname") after defining the flag.

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

How do you define arguments validation in Cobra?

A

Use the Args field in the command struct, such as cobra.ExactArgs(1) or custom functions.

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

What function starts Cobra command execution?

A

rootCmd.Execute() is called from main() to start CLI parsing and command routing.

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

How do you print help text in a Cobra command?

A

Use cmd.Help() or run the command with --help to print usage and flag info.

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

Front

A

Back

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

What is Charmbracelet’s huh library used for?

A

huh is a TUI (text UI) form library that makes it easy to collect structured CLI input using forms and steps.

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

How do you start a new huh form?

A

Use form.New() and chain AddFields(...) to define input steps.

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

How are field values accessed after running a huh form?

A

Each field is passed by reference (like &name) and populated after form.Run() completes.

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

What types of fields does huh support?

A

Input, Select, MultiSelect, Confirm, Group, Text, Spacer, and custom fields.

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

How do you customize a field’s behavior in huh?

A

Use options like .Title(...), .Validate(...), .Options(...), or .Key(...) to control layout and behavior.

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

How do you validate user input in huh?

A

Use .Validate(func(string) error) to reject invalid input and show an error message.

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

What does form.WithTheme(...) do in huh?

A

It sets the visual theme for the entire form UI.

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

What function runs the huh form and returns an error?

A

form.Run() runs the form and returns an error if the user cancels or there’s a display issue.

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

Front

A

Back

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

What is the purpose of go generate?

A

It runs arbitrary shell commands specified in special //go:generate comments in Go source files.

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

What’s a typical comment format for go generate?

A

//go:generate command args — written above the code it relates to, usually above a type or package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you run `go generate` for a package?
`go generate ./...` or `go generate` in the desired package directory.
26
What is `stringer` used for in Go?
It generates a `String()` method for enums (const groups), improving their readability in logs and fmt.
27
What is `go:embed` used for?
It embeds static files or strings directly into the Go binary at compile time.
28
How do you use `go:embed` to include a file?
```go //go:embed config.json var config string ```
29
What is a common use of `go:generate` in protobuf or gRPC workflows?
To automate `protoc` compilation steps into Go structs and services.
30
How can you check that generated Go code is up to date?
Use `go generate` in CI and compare to version-controlled files, or use tools like `go:generate` with checksum guards.
31
Front
Back
32
What is the purpose of `go generate`?
It runs arbitrary shell commands specified in special `//go:generate` comments in Go source files.
33
What’s a typical comment format for `go generate`?
`//go:generate command args` — written above the code it relates to, usually above a type or package.
34
How do you run `go generate` for a package?
`go generate ./...` or `go generate` in the desired package directory.
35
What is `stringer` used for in Go?
It generates a `String()` method for enums (const groups), improving their readability in logs and fmt.
36
What is `go:embed` used for?
It embeds static files or strings directly into the Go binary at compile time.
37
How do you use `go:embed` to include a file?
```go //go:embed config.json var config string ```
38
What is a common use of `go:generate` in protobuf or gRPC workflows?
To automate `protoc` compilation steps into Go structs and services.
39
How can you check that generated Go code is up to date?
Use `go generate` in CI and compare to version-controlled files, or use tools like `go:generate` with checksum guards.
40
Front
Back
41
What is the idiomatic way to handle errors in Go?
Check the error immediately after the call: ```go if err != nil { return err } ```
42
What Go idiom is used to wrap errors with context?
Use `fmt.Errorf` with `%w`: ```go return fmt.Errorf("failed to open file: %w", err) ```
43
What does `errors.Is(err, target)` do?
Checks whether `err` is or wraps `target`.
44
What does `errors.As(err, &target)` do?
Attempts to cast an error to a specific type. Sets `target` to the error if possible.
45
What’s the simplest way to define and use an interface?
Define the interface and implement its methods: ```go type Speaker interface { Speak() } ```
46
What’s the benefit of defining small, focused interfaces in Go?
They’re easier to implement and compose. Promotes decoupling and flexibility.
47
How do you check at compile time that a type implements an interface?
Use a dummy assignment: ```go var _ MyInterface = (*MyType)(nil) ```
48
What’s the Go convention for naming interfaces?
Use an `-er` suffix if possible (e.g., `Reader`, `Writer`, `Closer`).
49
How do you write a basic test function in Go?
Use `func TestXxx(t *testing.T)` in a `_test.go` file. Run it with `go test`.
50
How do you skip a test in Go?
Call `t.Skip()` or `t.Skipf()` inside the test function.
51
How do you fail a test in Go?
Use `t.Error()`, `t.Errorf()`, `t.Fatal()` or `t.Fatalf()`.
52
What’s the difference between `t.Error()` and `t.Fatal()`?
`t.Error()` reports an error but continues. `t.Fatal()` stops the test immediately.
53
How do you run a specific test function in Go?
Use `go test -run ^TestName$` to run just that test.
54
Front
Back
55
What is the idiomatic way to handle errors in Go?
Check the error immediately after the call: ```go if err != nil { return err } ```
56
What Go idiom is used to wrap errors with context?
Use `fmt.Errorf` with `%w`: ```go return fmt.Errorf("failed to open file: %w", err) ```
57
What does `errors.Is(err, target)` do?
Checks whether `err` is or wraps `target`.
58
What does `errors.As(err, &target)` do?
Attempts to cast an error to a specific type. Sets `target` to the error if possible.
59
What’s the simplest way to define and use an interface?
Define the interface and implement its methods: ```go type Speaker interface { Speak() } ```
60
What’s the benefit of defining small, focused interfaces in Go?
They’re easier to implement and compose. Promotes decoupling and flexibility.
61
How do you check at compile time that a type implements an interface?
Use a dummy assignment: ```go var _ MyInterface = (*MyType)(nil) ```
62
What’s the Go convention for naming interfaces?
Use an `-er` suffix if possible (e.g., `Reader`, `Writer`, `Closer`).
63
How do you write a basic test function in Go?
Use `func TestXxx(t *testing.T)` in a `_test.go` file. Run it with `go test`.
64
How do you skip a test in Go?
Call `t.Skip()` or `t.Skipf()` inside the test function.
65
How do you fail a test in Go?
Use `t.Error()`, `t.Errorf()`, `t.Fatal()` or `t.Fatalf()`.
66
What’s the difference between `t.Error()` and `t.Fatal()`?
`t.Error()` reports an error but continues. `t.Fatal()` stops the test immediately.
67
How do you run a specific test function in Go?
Use `go test -run ^TestName$` to run just that test.
68
Front
Back
69
What does `defer` do in Go?
`defer` schedules a function call to run after the surrounding function returns. Example: defer file.Close()
70
In what order do deferred functions execute?
Deferred functions are executed in **Last In, First Out (LIFO)** order.
71
What is a common use case for `defer`?
Resource cleanup, such as closing files, unlocking mutexes, or stopping timers.
72
How do you start a goroutine?
Use the `go` keyword before a function call: ```go go doSomething() ```
73
Why doesn’t a program with only goroutines run indefinitely?
The main function exits, ending the program even if goroutines are still running. Use sync tools or `select{}` to block.
74
What happens if a goroutine panics and is unhandled?
It terminates silently unless caught with `recover()` inside a `defer`.
75
What is the type signature of `io.Reader`?
```go type Reader interface { Read(p []byte) (n int, err error) } ```
76
How do you read the entire contents of an `io.Reader`?
Use `io.ReadAll(r)` or `io.Copy(dst, r)` if streaming to another writer.
77
What is the idiomatic way to wrap an `io.Reader` with a buffer?
Use `bufio.NewReader(r)` to reduce system calls and improve performance.
78
What does `select {}` do in Go?
It blocks forever. Often used to prevent `main()` from exiting when running goroutines.
79
What is a race condition in Go?
When two or more goroutines access shared memory concurrently and at least one writes to it without proper synchronization.
80
How do you detect race conditions in Go code?
Run tests or programs with the `-race` flag: `go run -race main.go`
81
How can `sync.WaitGroup` help with goroutines?
It waits for a collection of goroutines to finish, avoiding premature exit of `main()`.