Go Flashcards

1
Q

In Go, every program is part of a…

A

Package

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

What does the first line of code in Go program look like?

A

package main

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

What lets us import files included in the fmt package?

A

import “fmt”

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

What is considered white space?

A

Blank lines

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

What is the purpose of leaving white space?

A

Makes code more readable

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

What is func main() {

}
?

A

A function. Any code within the {} is executed.

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

In Go, executable code belongs to what package?

A

main

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

How are statements separated?

A

Ending a line by hitting enter key or by a semicolon “;”

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

What is a comment?

A

Text that is ignored upon execution. Used to explain code and make it more readable.

They can be used to prevent code execution when testing an alternative code, as well.

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

How are comments denoted?

A

// single line comments

/* multi line comments */

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

What are the three basic data types? What do they represent?

A

bool: boolean values, true/false

Numeric: integer types, floating point values, and complex types

string: string values

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

How do you declare a variable?

A

var variablename type = value

variablename := value

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

:=

A

When this declaration is used, type of variable is inferred.

It’s not possible to declare a variable using := without assigning a value to it.

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

What happens when a variable is declared without an initial value?

A

Values get set to default value of its type.

string = “ “
int = 0
bool = false

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

Can you declare a variable using “:=“ without assigning a value to it?

A

No

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

What is the difference between the var and := ?

A

var
Can be used inside and outside of functions
Variable declaration and value assignment can be done separately

:=
Can only be used inside functions
Variable declaration and value assignment cannot be done separately (must be done on same line)

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

Multiple Variable Declaration

A

It is possible to declare multiple variables in same line.

Only possible to declare one type of variable per line

var a, b, c, d int = 1, 3, 5, 7

var (
a int
b int = 1
c string = “hello
)

If type keyword is not specified, different types of variables can be declared in the same line

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

Go variable naming rules

A

Must start with a letter or _
Cannot start with digit
Can only contain alpha numeric and _
Names are case sensitive!!
No length limit
Cannot be Go keywords

19
Q

Camel Case

A

myVariableName = “John”

20
Q

Pascal Case

A

MyVariableName = “John”

21
Q

Snake Case

A

my_variable_name = “John”

22
Q

How is Boolean data type declared?

A

bool

23
Q

What value can bool values take?

A

True or false

(Defaults to false if not declared)

24
Q

What are Boolean values used for?

A

Mostly conditional testing

25
Q

What do Integer data types do?

A

Used to store a whole number without decimals

26
Q

What is a signed integer?

A

int

Can store both positive and negative values

27
Q

What is an unsigned integer?

A

unit

Can only store non negative values

28
Q

Types of signed integers

A

int 32 or 64 depends on system
int8 -128 to 127
int16 -32768 to 32767
int32 -2147483648 to 2147483647
int64 very large

29
Q

Types of unsigned integers

A

uint depends on system
uint8 0 to 255
uint16 0 to 65535
uint32 0 to 4294967295
uint64 Very large

30
Q

What is a float data type?

A

Used to store positive and negative numbers with decimal points

Default is float64

31
Q

Types of float data types

A

float32 32 bits -3.4e+38 to “+

float64 64 bits -1.7e+308 to “+

32
Q

What is string data type?

A

Used to store a sequence of characters (text).

Must be surrounded by double quotes “ “

Default if no value is “ “

33
Q

What is a constant?

A

Assigned with const; becomes read-only. Value must be assigned when declared.

Used when variable should have fixed value that cannot be changed.

Usually written in all UPPER for easy identification and differentiation from other variables

34
Q

Constant types

A

Types constants: declared with a defined type

Untyped constants: declared without a type

35
Q

Functions to output text

A

Print()
Println()
Printf()

36
Q

Print() Function

A

Prints its arguments with default formatting

37
Q

How to print arguments in new lines?

A

\n

var i, j string = “Hello”, “World”

fmt.Print(i, “\n”)
fmt.Print(j, “\n”)

Prints:
Hello
World

Also:

fmt.Print(i, “ “, j)
fmt.Print(i, “\n”, j)
Will default with spaces if type is not a string

38
Q

\n

A

Creates new line

39
Q

Println() Function

A

White space is added between arguments and a new line added at the end of the

40
Q

Printf() Function

A

Formats its argument based on the given formatting verb and then prints them

Ex of formatting verbs:
%v used to print value of argument
%T used to print type of argument

fmt.Print(“i has value: %v and type: %T\”, i, j)

41
Q

%v

A

Prints the value in the default format

42
Q

%#v

A

Prints the value in go-syntax format

43
Q

%T

A

Prints type of the value

44
Q

%%

A

Prints the % sign