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.
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.
How to make the string index to return a char instead of a byte?
Use the array notation: 0:1
What library compares strings? What does it return if two strings are the same?
strings.Compare
returns 0
How to compare strings case insensitive?
strings.EqualFold
How to replace a string in go?
strings.Replace(full strings, sub string, how many times to replace… -1 all of them)
What library deals with regular expressions?
regexp
What is title case? How to do that?
All words starts with capital letter. strings.Title(“asd”).
What is introspection?
Introspection is the ability of the application to examine , introspect and modify its own structure and behavior.
What is a basic type?
primitive types: bool, integer and etc.
What is an aggregate type? Give two examples.
Types that are made of one or more types (including basic types) to make an object. Arrays and structs.
What is an reference type? Give three examples.
Reference memory where the data is stored. Pointers, slices and channels.
What is an interface?
Set of methods that can be used by concrete types (struct).
What does an empty interface?
Means that a method can receive any argument.
What happens when you change the content of a struct?
It creates a new copy of the struct.
How to create a private field and a public field in a struct?
type Person struct {
privateName string
PublicName string
}
When can I set a private struct field in go?
In its constructor.
Which package handles reflection?
reflect
How to get the name of a type? How to get all the values within a structure? How to know which kind of type is any type we want?
reflect.typeOf(), reflect.ValueOf(), reflect.TypeOf().Kind()
How to create a new type on runtime?
make([]Employee, 3)