The Go Standard Library Flashcards
(42 cards)
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.
how to get the time now? Does it return wall clock or monotonic time?
time.Now() Returns both (inside a struct)
How to format the date?
t := time.Now()
t.Format(time.Kitchen)
How to sleep in go? What is the unit of measure?
time. Sleep()
nanoseconds. .. 1000000000 = 1 second
What function to use to know the elapsed time in go? How to get the time until something needs to happen?
time. Since(myDate)
time. Until(myDate)
What does the string package do?
All strings manipulation.
What is a string in go?
A read-only slice of bytes
What goes do when the string is modified? Does it change the existing strings in memory? why?
Creates a copy and do the modifications in the new memory location.
no because it is a read-only slice of bytes.
Are strings indexed? Where does the index start? Does it return a char?
Yes. 0. No, returns a byte.