Go Lang Flashcards

1
Q

Is GO a compiled or interpreted language?

A

Go is a compiled language

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

Go is either compiled or interpreted. What is a benefit or feature that GO provides that is unlike other languages executed similarly to GO?

A

Go, although being a compiled language, still has a garbage collector, which makes memory management easy

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

GO doesn’t have classes what does it have in its stead and what are some of the features of classes that GO is not able to implement?

A

GO doesn’t have classes but has structs instead. Structs don’t support inheritance, constructors, and generics. This makes it easier to code and faster to run.

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

Are variables case-sensitive in GO?

A

yes they are

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

How to declare variables in GO?

A

var x, y int
keyword var_name_comma_sep type

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

What’s a way to initialize and declare variables together in GO?

A

you can either do
var x int =100
or
var x = 100 //infers that 100 is int and assigns accordingly
or
x := 100 // can only be done inside a function; its illegal outside

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

What are * and & in reference to pointers?

A

The “*” operator is used to declare a pointer variable and to access the value stored at the memory address pointed by the pointer.
The “&” operator is used to get the memory address of a variable. It is also called the address-of-operator.

func main() {
x := 10 // Declare variable x with value 10
var ptr int // Declare a pointer variable ptr of type int
ptr = &x // Assign the memory address of x to ptr
fmt.Println(“x:”, x) // Prints x: 10
fmt.Println(“ptr:”, ptr) // Prints ptr: 0x10414120
fmt.Println(“
ptr:”, *ptr) // Prints *ptr: 10
*ptr = 20 // Change the value stored at the memory address pointed by ptr
fmt.Println(“x:”, x) // Prints x: 20
}

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

what does the new() function do?

A

It returns a pointer to a variable.

use case:
ptr := new(int)
*ptr = 3

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

Does dynamically and statically allocated memory go in a heap or stack?

A

Dynamically allocated memory in a heap and statically allocated memory in the stack.

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

what is iota in GoLang?

A

iota is used when you want to define a set of constants which each have a different value but you don’t really care about what the value is.

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

what’s is the typical form of a switch statement

A

switch variable {
case value1:
// code to be executed if variable == value1
case value2:
// code to be executed if variable == value2
case value3:
// code to be executed if variable == value3

default:
// code to be executed if variable does not match any of the case values
}

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

what happens in Go if you write a switch statement without a tag?

A

it executes the first case that evaluates to true

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

what is scan in Go and how is it used

A

scan is a method provided by the fmt package in Go, which is used to read input from a user and parse it into variables. It is commonly used to read input from the standard input (stdin)

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

how do you define a slice in GoLang

A

for slices using [] is main
var my_slice[]int
or
my_slice := []int
or
weekdays := make([]string, 5)
or []string
or
[]string{}
or
[]string{value1, value2, value3, …value n}
arr := [7]string{“This”, “is”, “the”, “tutorial”, “of”, “Go”, “language”}

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

first.go
package main

import “fmt”

func main() {

var a1 byte = 97
var a2 byte = 98
var a3 byte = 99

fmt.Println(a1)
fmt.Println(a2)
fmt.Println(a3)

fmt.Printf("%c\n", a1)
fmt.Printf("%c\n", a2)
fmt.Printf("%c\n", a3) }
A

$ go run first.go
97
98
99
a
b
c

A byte in Go is an unsigned 8-bit integer. It has type uint8. A byte has a limit of 0 – 255 in numerical range. It can represent an ASCII character.
Go uses rune, which has type int32, to deal with multibyte characters.

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

types.go
package main

import (
“fmt”
“reflect”
)

func main() {

 var a byte = 97
 var b = 98
 c := 'c'

 fmt.Println(a)
 fmt.Println(b)
 fmt.Println(c)

 fmt.Println("-------------------------")

 fmt.Printf("%c\n", a)
 fmt.Printf("%c\n", b)
 fmt.Printf("%c\n", c)

 fmt.Println("-------------------------")

 fmt.Println(reflect.TypeOf(a))
 fmt.Println(reflect.TypeOf(b))
 fmt.Println(reflect.TypeOf(c)) }
A

$ go run types.go
97
98
99
————————-
a
b
c
————————-
uint8
int
int32

Go uses rune, which has type int32, to deal with multibyte characters.
A byte in Go is an unsigned 8-bit integer. It has type uint8. A byte has a limit of 0 – 255 in numerical range.

17
Q

what are slices in GO?

A

You can think of a slice in go as a pointer to an array with an offset and length. It can be passed by value to functions and not just by reference.