Go

CLI Development

CLI design, flag package, cobra/viper, structuring CLI apps, exit codes, stdin/stdout/stderr, UX patterns, testing CLI apps

20 domande da colloquioยท
Senior
1

What is the main role of the flag package in Go?

Risposta

The flag package provides a simple API for parsing command-line arguments. It allows defining flags with predefined types (string, int, bool, etc.) and automatically handles parsing and validation. This package is part of the standard library and is suitable for simple CLIs.

2

How to define a string flag with the flag package and retrieve its value?

Risposta

The flag.String function returns a pointer to the flag value. After calling flag.Parse(), the value can be accessed by dereferencing the pointer. It's also possible to use flag.StringVar to bind directly to an existing variable.

3

What is the difference between flag.String and flag.StringVar?

Risposta

flag.String allocates a new variable and returns a pointer to it, while flag.StringVar binds the flag to an existing variable passed by address. StringVar is useful when the variable needs to be defined at package level or when you want to avoid dereferencing.

4

What exit code should be used when a CLI application encounters an invalid arguments error?

5

What is the main advantage of Cobra over the standard flag package?

+17 domande da colloquio

Padroneggia Go per il tuo prossimo colloquio

Accedi a tutte le domande, flashcards, test tecnici, esercizi di code review e simulatori di colloquio.

Inizia gratis