The Go Standard Library Flashcards
What are the two ways to print data to the console app? What are the diffs?
println and printf. Printf will format and replace value of %v, won’t break line, needs a \n
How to printf with float?
%.2f
What is the difference between a flag and an arg? How to add a flag to our app?
Flags are named and args are arbitrary.
flag. String(“flagName”, “defOption”, “Description”)
flag. Parse
How to read keyboard input? When the input is read and the function returns?
fmt.Scanf
When the user press enter (\n)
How to read a text file for example?
bufio.NewScanner(file via os.Open)
Scanner.Scan {}
What does the fmt package do?
Format input and output text of our application. Can add padding and colors for example.
What does fmt.sscanf do?
get text from specific places
from a string that matches a pattern… e.g: hello %s, welcome to %s
How to add a table look like to cmd?
|%7s|%7s|%7s|
What does the log.println does? How to save the log to a file instead of cmd?
Print a string with the date and time on it (military format).
os.OpenFile(“asd.txt”, os.O_CREATE|os.O_APPEND|os.WRONLY, 0666) then log.SetOutput
what does the logFatal does?
Log a message and closes and kills the application.
Can I create one logger for each error level using the same file?
Yes. Just call log.New
How to trace the whole execution of my app to collect metrics/profiling and other low-level information. How to read a trace file?
trace.Start(file here).
With the trace tool: go tool trace trace.out
What does the time package do?
Get time, format time and calculate time.
When to use the wall clock? When the wall clock useful and not useful?
current time of the day.
Useful for human-readable time.
Not useful to calculate time spans.
When to use the monotonic clock?
- to measure time
- not subject to variation
Why using wall clock time is not as reliable as monotonic time?
Because ntp can kick in and change the OS clock affecting all measurements.