The Go Standard Library Flashcards

1
Q

What are the two ways to print data to the console app? What are the diffs?

A

println and printf. Printf will format and replace value of %v, won’t break line, needs a \n

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

How to printf with float?

A

%.2f

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

What is the difference between a flag and an arg? How to add a flag to our app?

A

Flags are named and args are arbitrary.

flag. String(“flagName”, “defOption”, “Description”)
flag. Parse

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

How to read keyboard input? When the input is read and the function returns?

A

fmt.Scanf

When the user press enter (\n)

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

How to read a text file for example?

A

bufio.NewScanner(file via os.Open)

Scanner.Scan {}

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

What does the fmt package do?

A

Format input and output text of our application. Can add padding and colors for example.

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

What does fmt.sscanf do?

A

get text from specific places

from a string that matches a pattern… e.g: hello %s, welcome to %s

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

How to add a table look like to cmd?

A

|%7s|%7s|%7s|

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

What does the log.println does? How to save the log to a file instead of cmd?

A

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

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

what does the logFatal does?

A

Log a message and closes and kills the application.

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

Can I create one logger for each error level using the same file?

A

Yes. Just call log.New

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

How to trace the whole execution of my app to collect metrics/profiling and other low-level information. How to read a trace file?

A

trace.Start(file here).

With the trace tool: go tool trace trace.out

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

What does the time package do?

A

Get time, format time and calculate time.

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

When to use the wall clock? When the wall clock useful and not useful?

A

current time of the day.
Useful for human-readable time.
Not useful to calculate time spans.

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

When to use the monotonic clock?

A
  • to measure time

- not subject to variation

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

Why using wall clock time is not as reliable as monotonic time?

A

Because ntp can kick in and change the OS clock affecting all measurements.

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

how to get the time now? Does it return wall clock or monotonic time?

A
time.Now()
Returns both (inside a struct)
18
Q

How to format the date?

A

t := time.Now()

t.Format(time.Kitchen)

19
Q

How to sleep in go? What is the unit of measure?

A

time. Sleep()

nanoseconds. .. 1000000000 = 1 second

20
Q

What function to use to know the elapsed time in go? How to get the time until something needs to happen?

A

time. Since(myDate)

time. Until(myDate)

21
Q

What does the string package do?

A

All strings manipulation.

22
Q

What is a string in go?

A

A read-only slice of bytes

23
Q

What goes do when the string is modified? Does it change the existing strings in memory? why?

A

Creates a copy and do the modifications in the new memory location.
no because it is a read-only slice of bytes.

24
Q

Are strings indexed? Where does the index start? Does it return a char?

A

Yes. 0. No, returns a byte.

25
Q

How to make the string index to return a char instead of a byte?

A

Use the array notation: 0:1

26
Q

What library compares strings? What does it return if two strings are the same?

A

strings.Compare

returns 0

27
Q

How to compare strings case insensitive?

A

strings.EqualFold

28
Q

How to replace a string in go?

A

strings.Replace(full strings, sub string, how many times to replace… -1 all of them)

29
Q

What library deals with regular expressions?

A

regexp

30
Q

What is title case? How to do that?

A

All words starts with capital letter. strings.Title(“asd”).

31
Q

What is introspection?

A

Introspection is the ability of the application to examine , introspect and modify its own structure and behavior.

32
Q

What is a basic type?

A

primitive types: bool, integer and etc.

33
Q

What is an aggregate type? Give two examples.

A

Types that are made of one or more types (including basic types) to make an object. Arrays and structs.

34
Q

What is an reference type? Give three examples.

A

Reference memory where the data is stored. Pointers, slices and channels.

35
Q

What is an interface?

A

Set of methods that can be used by concrete types (struct).

36
Q

What does an empty interface?

A

Means that a method can receive any argument.

37
Q

What happens when you change the content of a struct?

A

It creates a new copy of the struct.

38
Q

How to create a private field and a public field in a struct?

A

type Person struct {
privateName string
PublicName string
}

39
Q

When can I set a private struct field in go?

A

In its constructor.

40
Q

Which package handles reflection?

A

reflect

41
Q

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?

A

reflect.typeOf(), reflect.ValueOf(), reflect.TypeOf().Kind()

42
Q

How to create a new type on runtime?

A

make([]Employee, 3)