1/278
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
how to run tests
go test
how to decide what to test
ask yourself what do you really care about?
what is the syntax for writing a test
func TestBlah(t *testing.T) {}
what needs to be listed at the top of every file
package main or a different package
what is the second thing listed on every file
a list of imports ()
what is a receiver function
a function that works on a specific type. think of it like a method on a class.
what is the syntax for a receiver functions
func (t thing) nameofFunc() {}
how to you call a receiver function
thing.nameOfFunc()
what is a struct
it's like an object that holds fields of certain types
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
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,
how do you turn an address into a value
but using a pointer to the address -> *address
how do you turn a value into an address
by dereferencing the value -> &value
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
given name := "Bill"; fmt.Println(&name) what is printed
the memory address that Bill is stored at
what is the '&' operator used for
turning a value into a pointer to an address in memory
When you see a * operator in front of a pointer, what will it turn the pointer into?
a value
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
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
difference data types are reference types and which are value types
see image
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
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
Is the following a valid way of initializing and assigning a value to a variable?
var bookTitle string = "Harry Potter"
yes
Is the following a valid way of initializing an assigning a value to a variable?
fruitCount := 5
yes
will the following code compile:
paperColor := "Green"
paperColor := "Blue"
no, because a variable can only be initialized one time
does []int{} represent a slice where each element in it is of type int?
yes
is this valid code:
colors := []string{"Red", "Yellow", "Blue"}
yes
How do we iterate through each element in a slice and print out its value?
colors := []string{"blue", "red", "green"}
for i, v := range colors {
fmt.Println(i, v)
}
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
By creating a new type with a function that has a receiver, we...
are adding a 'method' to any value of that type
In the following snippet, what does the variable 'ls' represent?
type laptopSize float
func (ls laptopSize) getSizeOfLaptop() {
return ls
}
a value of type laptopSize
what is a map
a collection of a key (same type) value (same type) pairs
how do you declare and initialize a new map with values
newMap := map[type-of-keys]type-of-values{
"key": "pair",
"key": "pair",
}
how do you initialize an empty map
newMap := make(map[type-of-keys]type-of-values)
how do you add values to an initialized empty map
newMap["key"] = "value"
how do you delete a key/value pair from a map
delete(nameOfMap, "nameOfKey")
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
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
how do you iterate over a map
for key, value := range nameOfMap {
// do something
}
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
how do you define an interface
type nameOfInterface {
funcName(args) (return types)
}
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.
what are interfaces
see image
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
To say that a type satisfies an interface means that...
the type implements all of the functions contained in the interface definition
Types that implement the Reader interface are generally used to...
read information from an outside data source into our application
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
what does launching multiple go routines solve
helps a chunk of code run in parallel so we don't get blocked
how do you up another go routine
use the go keyword in front of a function
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
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
what is parallelism
having multiple threads executed at the exact same time. this requires multiple CPUs
what are channels
channels are used to communicate between go routines routines. channels are typed
how do you create a channel
c := make(chan string )
how do you send data with channels
see pic
true/false - the main go routine waiting for a channel msg is a blocking call
true
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
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.
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.
A return statement without arguments returns the named return values. This is known as a "naked" return.
Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.
example:
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
different ways to declare variable
using var
ex. no initializer: var a, b, c bool
ex. w/ initializer: var a, b = 1 , 2
type will be inferred
using short assignment (inside a function only)
ex: k := 3
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
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
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.
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:
what are the zero values for numeric, boolean, and string types
numeric = 0