Golang Flashcards
Go: Import package “fmt” and “math/rand” using ()
import ( “fmt”, “math/rand” )
Go: How to call pi value in math using exported names? (math.???)
math.Pi
Go: Create function with integer addition for (x,y)
func add(x int, y int) int { return x + y }
Go: Create function that returns quotient and remainder for (x,y)
func divide(x, y int) (int, int) { return x / y, x % y }
Go: Create function that returns quotient and remainder using named returns
func divide(x, y int) (q, r int) { q = x / y r = x % y return }
Go: How to simplify var x int, y int
var x, y int
Go: Initialize height, name, male (bool)
height, name, male := 174, “haidar”, true
var i int var str string var bol bool
What is the value of i, str, and bol?
i = 0
str = “”
bol = false
var i int = 42
Convert i
to float64
float64(i)
v := 42.3 // change me! fmt.Printf("v is of type %T\n", v)
What’s the output?
v is of type float64
Declare constant float64 of pi
const Pi = 3.14
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?
const x = 10
Write a loop that print out (using fmt.Println) 1 to 10
for i := 1; i <= 10; i++ { fmt.Println(i) }
Using while in go, print out (using fmt.Println) 1 to 10
i := 1 for i <= 10 { fmt.Println(i) i++ }
Write infinite loop in go!
for { }