Golang

0.0(0)
studied byStudied by 5 people
0.0(0)
call with kaiCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/278

flashcard set

Earn XP

Description and Tags

Golang flashcards

Last updated 5:20 PM on 12/26/23
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

279 Terms

1
New cards

how to run tests

go test

2
New cards

how to decide what to test

ask yourself what do you really care about?

3
New cards

what is the syntax for writing a test

func TestBlah(t *testing.T) {}

4
New cards

what needs to be listed at the top of every file

package main or a different package

5
New cards

what is the second thing listed on every file

a list of imports ()

6
New cards

what is a receiver function

a function that works on a specific type. think of it like a method on a class.

7
New cards

what is the syntax for a receiver functions

func (t thing) nameofFunc() {}

8
New cards

how to you call a receiver function

thing.nameOfFunc()

9
New cards

what is a struct

it's like an object that holds fields of certain types

10
New cards

what is RAM

memory is like a bunch of little containers. each has an address where the data is held. you can reference the address in order to access the data

11
New cards

is go a "pass by value" language

yes. anytime a parameter or argument is passed into a function, the values are copied to another variable in memory and then the copied object is passed,

12
New cards

how do you turn an address into a value

but using a pointer to the address -> *address

13
New cards

how do you turn a value into an address

by dereferencing the value -> &value

14
New cards

Whenever you pass an integer, float, string, or struct into a function, what does Go do with that argument?

it creates a copy of each argument and these copies are used inside the function

15
New cards

given name := "Bill"; fmt.Println(&name) what is printed

the memory address that Bill is stored at

16
New cards

what is the '&' operator used for

turning a value into a pointer to an address in memory

17
New cards

When you see a * operator in front of a pointer, what will it turn the pointer into?

a value

18
New cards

When we create a slice, Go will automatically create which two data structures?

an array and a structure that records the length of the slice, the capacity of the slice (how many elements), and a reference to the underlying array

19
New cards

yes or no - with 'value types' in Go, do we have to worry about pointers if we want to pass a value to a function and modify the original value inside the function?

yes

20
New cards

difference data types are reference types and which are value types

see image

21
New cards

yes or no - With 'reference types' in Go, do we have to worry about pointers if we want to pass a value to a function and modify the original value inside the function?

no

22
New cards

Is a slice a 'value type' or a 'reference type'

reference type because a slice contains a reference to the actual underlying list of records

23
New cards

Is the following a valid way of initializing and assigning a value to a variable?

24
New cards

var bookTitle string = "Harry Potter"

yes

25
New cards

Is the following a valid way of initializing an assigning a value to a variable?

26
New cards

fruitCount := 5

yes

27
New cards

will the following code compile:

28
New cards

paperColor := "Green"

29
New cards

paperColor := "Blue"​

no, because a variable can only be initialized one time

30
New cards

does []int{} represent a slice where each element in it is of type int?

yes

31
New cards

is this valid code:

32
New cards

colors := []string{"Red", "Yellow", "Blue"}

yes

33
New cards

How do we iterate through each element in a slice and print out its value?

colors := []string{"blue", "red", "green"}

34
New cards

for i, v := range colors {

35
New cards

fmt.Println(i, v)

36
New cards

}

37
New cards

Can a slice have both values of type 'int' and of type 'string' in it

no, a slice can only have one type in it

38
New cards

By creating a new type with a function that has a receiver, we...

are adding a 'method' to any value of that type

39
New cards

In the following snippet, what does the variable 'ls' represent?

40
New cards

type laptopSize float

41
New cards

func (ls laptopSize) getSizeOfLaptop() {

42
New cards

return ls

43
New cards

}

a value of type laptopSize

44
New cards

what is a map

  • a collection of a key (same type) value (same type) pairs

45
New cards

how do you declare and initialize a new map with values

newMap := map[type-of-keys]type-of-values{

46
New cards

"key": "pair",

47
New cards

"key": "pair",

48
New cards

}

49
New cards

how do you initialize an empty map

newMap := make(map[type-of-keys]type-of-values)

50
New cards

how do you add values to an initialized empty map

newMap["key"] = "value"

51
New cards

how do you delete a key/value pair from a map

delete(nameOfMap, "nameOfKey")

52
New cards

how is a map different than a struct

maps: use when collection have closely related properties, all keys and all values must have same type, don't need to know all keys at compile time, keys are indexed so you can iterate over them, they are a reference type

53
New cards

structs: values can be different types, you need to know all the different fields at compile time, keys don't support indexing so you can't iterate over them, use a struct to represent a "thing" with a lot of different properties, are a reference type

54
New cards

how do you iterate over a map

for key, value := range nameOfMap {

55
New cards

// do something

56
New cards

}

57
New cards

when do we use interfaces

we use interfaces to define a method set or a function set. it helps reuse functions and avoid some of the type madness. it's like an adapter to take diff sources of input and translate them into a common medium. Look up the Reader interface

58
New cards

how do you define an interface

type nameOfInterface {

59
New cards

funcName(args) (return types)

60
New cards

}

61
New cards

what is a concrete type vs. an interface type

with concrete types (maps, structs, int, string, etc.) you can create value out of them. with an interface type you cannot create value directly out of them.

62
New cards

what are interfaces

see image

63
New cards

When we say that interfaces can be satisfied implicitly, we mean that..

we don't have to write extra code to say that some type satisfies an interface

64
New cards

To say that a type satisfies an interface means that...

the type implements all of the functions contained in the interface definition

65
New cards

Types that implement the Reader interface are generally used to...

read information from an outside data source into our application

66
New cards

what is a go routine

executes the program line by line. running a program automatically kicks off a main go routine. creating another go routine can be used to handle blocking code

67
New cards

what does launching multiple go routines solve

helps a chunk of code run in parallel so we don't get blocked

68
New cards

how do you up another go routine

use the go keyword in front of a function

69
New cards

by default Go tries to execute using how many CPU (central processing unit) cores

only one unless you override it. so even if you have multiple routines the program isn't truly running in parallel

70
New cards

what is concurrency

has the ability have multiple threads (or go routines) executing code. if one thread finished/dies, another one is picked up and worked on

71
New cards

what is parallelism

having multiple threads executed at the exact same time. this requires multiple CPUs

72
New cards

what are channels

channels are used to communicate between go routines routines. channels are typed

73
New cards

how do you create a channel

c := make(chan string )

74
New cards

how do you send data with channels

see pic

75
New cards

true/false - the main go routine waiting for a channel msg is a blocking call

true

76
New cards

what is a function literal in go

it's just like an anonymous function in javascript, or a lambda in python. It's an unnamed function that we use to wrap a chunk of code so that we can execute it at some point in the future

77
New cards

How are packages imported / named

the package name is the same as the last element of the import path. For instance, the "math/rand" package comprises files that begin with the statement package rand.

78
New cards

Can returned values be named

yes, but they don't have to be. If they are names then they should be treated as variables at the top of the function.

79
New cards
80
New cards

A return statement without arguments returns the named return values. This is known as a "naked" return.

81
New cards

Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.

82
New cards
83
New cards

example:

84
New cards
85
New cards

func split(sum int) (x, y int) {

86
New cards

x = sum * 4 / 9

87
New cards

y = sum - x

88
New cards

return

89
New cards

}

90
New cards

different ways to declare variable

  1. using var

91
New cards

ex. no initializer: var a, b, c bool

92
New cards

ex. w/ initializer: var a, b = 1 , 2

93
New cards

type will be inferred

94
New cards
  1. using short assignment (inside a function only)

95
New cards

ex: k := 3

96
New cards

true or false: Outside a function, every statement must begin with a keyword (var, func, and so on)

true. this means that the short variable assignment is not available

97
New cards

what are go's basic data types

bool, string, int, int8, int16, int..., byte (which is a uint8), rune (alias for int32 which represents a unicode code point), float 32, float64, complex64

98
New cards

what are the int, uint, and uintptr types

the represent integers. they are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

99
New cards

that do the << or >> operators mean

they are bitwise shift operators. so, x << y means x × 2y, while x >> y means x × 2−y. These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by a power of two has the effect of "shifting" the digits left or right, respectively:

100
New cards

what are the zero values for numeric, boolean, and string types

numeric = 0