Golang Flashcards

1
Q

Go: Import package “fmt” and “math/rand” using ()

A

import ( “fmt”, “math/rand” )

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

Go: How to call pi value in math using exported names? (math.???)

A

math.Pi

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

Go: Create function with integer addition for (x,y)

A

func add(x int, y int) int { return x + y }

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

Go: Create function that returns quotient and remainder for (x,y)

A

func divide(x, y int) (int, int) { return x / y, x % y }

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

Go: Create function that returns quotient and remainder using named returns

A

func divide(x, y int) (q, r int) { q = x / y r = x % y return }

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

Go: How to simplify var x int, y int

A

var x, y int

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

Go: Initialize height, name, male (bool)

A

height, name, male := 174, “haidar”, true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
var i int
var str string
var bol bool

What is the value of i, str, and bol?

A

i = 0

str = “”

bol = false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
var i int = 42

Convert i to float64

A
float64(i)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
v := 42.3 // change me!
fmt.Printf("v is of type %T\n", v)

What’s the output?

A

v is of type float64

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

Declare constant float64 of pi

A
const Pi = 3.14
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

I need an x that can be passed to these functions

func needFloat(x float64) float64 {
  return x / 3
}

func needInt(x int) int {
return x / 3
}
~~~
```

How to declare/initialize x?

A
const x = 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a loop that print out (using fmt.Println) 1 to 10

A
for i := 1; i <= 10; i++ {
	fmt.Println(i)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Using while in go, print out (using fmt.Println) 1 to 10

A
i := 1
for i <= 10 {
  fmt.Println(i)
  i++
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write infinite loop in go!

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

Output (using fmt.Println)

if x is below 3, print fizz.

if x is 3 or more, print buzz

A
if x < 3 {
	fmt.Println("fizz")
} else {
	fmt.Println("buzz")
}
17
Q

Create func mult(x, y, lim int) int

return x * y, but if more than lim, returns lim

A
func mult(x, y, lim int) int {
	if v := x * y; v < lim {
		return v
	}
	return lim
}
18
Q

Using switch, create a function that returns prefix (Mr. or Mrs.) with gender input

def func(gender string) (string)

A
func prefix(gender string) string {
	switch gender {
	case "male":
		return "Mr."
	case "female":
		return "Mrs."
	default:
		return ""
	}
}
19
Q

Using defer, print “hello world haidar” in func main() with reverse order in calling!

A
func main() {
        defer fmt.Println("haidar")
	defer fmt.Println("world")
	defer fmt.Println("hello")
}