Introduction Flashcards
Strings are ___ indexed in Go
0
Integer types in Go
uint8, uint16, uint32, uint64, int8, int16, int32, and int64
Three logical operators in Go
&& - and, || - or, ! - not
Rules for naming variables in Go
- Start with a latter
2. Can contain letters, numbers and _
Keyword for declaring a variable in Go
var
Keyword for declaring a constant in Go
const
Two ways of declaring variables in Go
- Using the var keyword inline
2. Using the var keyword with a block [ var (…) ]
What scoping rules does Go use?
Lexcial scoping
What options are available for controlling flow in Golang?
for, if and switch
How should you write a while loop in Go?
for {…} or for true {…}
What is the difference between “” and `` in Go?
Strings created from “” can contain only escape sequences. Strings created from `` can contain new lines and other literal.s
How are arrays initialized in go
var []
The ____ function can be used to find the length of arrays and strings in Go
len
To convert between types in Go, you ______
Use the type names like a function
We use the keyword _____ followed by the name of the variable we want to loop over
range
A _______ is used to tell the compiler that we don’t need this variable
single underscore (_)
Short syntax for creating arrays in Go
[]{…items…}
The length of the slice type in Go is constant. True or false?
False
What are the ways in which you can create a slice in Go?
- Using the make function
2. Indexing arrays (arr[low: high])
What are the arguments to the copy function?
dst (destination) and src (source)
How should maps be created in Go?
var := make(map[])
The _____ function can be used to delete items from a map
delete
What is a variadic parameter?
A parameter which can be used to accept multiple arguments of a single type
How to define
func (args …) {}
The ____ keyword can be used to move a function call to the end
defer
Deferred function calls happen before the function returns. True of false?
True
Deferred functions are run even if a runtime panic occurs. True or false?
True
In Go, arguments are passed by value by default unless pointers are used. True or false?
True
In Go, a pointer is represented using an ______ followed by the type of the storedvalue
asterisk
An asterisk is also used to dereference pointer variables. Dereferencing a pointer givesus access to the value the pointer points to. True or false?
True
What’s the difference between * and & in go?
- indicates that the object in question is a pointer of type . & is how this pointer is obtained.
The _____ function takes a type as an argument, allocates enough memory to fit a value of that type,and returns a pointer to it.
new
We can access fields of a struct using the ____ operator
.
Embedded types in Go are an implementation of inheritance. True or false?
True
How are embedded types defined?
type struct { …fields }
A _______ is a list of methods that a type must have in order to implement the inter‐face.
method set
What is a struct?
A struct is a container which contains has-a/is-a relationships.
What is an interface?
An interface is a container which defines a bunch of methods which can be called.
How are structs and interfaces related to each other?
If a struct is said to implement an interface, then the struct implements all the methods that the interface defines.
To search for a smaller string in a bigger string, use the ____ function
strings.Contains
To count the number of times a smaller string occurs in a bigger string, use the ______ function
strings.Count
To determine if a bigger string starts with a smaller string, use the ______ function
strings.HasPrefix
To determine if a bigger string ends with a smaller string, use the _____ function
strings.HasSuffix
To find the position of a smaller string in a bigger string, use the _______ function
strings.Index
To take a list of strings and join them together in a single string separated by anotherstring (e.g., a comma), use the __________ function
strings.Join
To repeat a string, use the _________ function
strings.Repeat
To replace a smaller string in a bigger string with some other string, use the _______ function, which also takes a number indicating how many times to do the replacement
strings.Replace
To split a string into a list of strings by a separating string (e.g., a comma), use the function
strings.Split
To convert a string to all lowercase letters, use the function
strings.ToLower
To convert a string to all uppercase letters, use the function
strings.ToUpper
To convert a string to a slice of bytes use the _____ function
byte
the io package has a ____ function that copies data from a Reader to a Writer
Copy
To read or write to a []byte or a string, you can use the ______ struct found in the ______ package:
Buffer, bytes
You can convert a Buffer into a []byte by calling ________
Buffer.Bytes()
To open a file in Go, use the _____ function from the __ package
Open, os
The _____ function from the ____ package serves as a shortcut to opening files using os.Open
ioutil.ReadFile
To create a file, use the _________ function.
os.Create
To get the contents of a directory, we use the same os.Open function but give it adirectory path instead of a file name. Then we call the ______ method
Readdir
The _____ function can be used to perform a specific set of operations (defined in a function) on each file and folder in a directory. The registered function is passed three arguments: ____, ____ and ___ for each file/folder in the directory.
os.Walk, path of the file, info of the file and err
We can create our own errors by using the _____ function in the _____ package
New, errors
The _____ package contains functions for sorting arbitrary data.
Sort
The non-cryptographic hash functions can be found underneath the ____ package
hash
The cryptographic hash functions can be found underneath the ____ package
crypto
In Go, we can create a TCP server using the ____ package’s _____ function
net, Listen
The ____ package allows us to parse arguments and flags sent to our program.
flag
To create a goroutine, we use the keyword ____ followed by a _________
go, function invocation