1. What are the two ways to create a new variable?
var x string = "Hello"
x := "Hello"
2. What is the value of x
after running x := 5; x += 1
?
6
3. What is scope and how do you determine the scope of a variable in Go?
The scope is the range of locations where a variable is usable. In Go a variable exists within the nearest curly braces.
4. What is the difference between var
and const
?
With var
we define variables the value of which we can later change.
With const
we define variables the value of which never changes.
5. Using the example program as a starting point, write a program that converts from
Fahrenheit into Celsius (C = (F - 32) * 5/9)
package main
import "fmt"
func main() {
fmt.Print("Enter Fahrenheit degrees: ")
var input float64
fmt.Scanf("%f", &input)
output := ((input - 32) * 5/9)
fmt.Println(output, "Celsius degrees")
}
6. Write another program that converts from feet into meters (1 ft = 0.3048 m)
package main
import "fmt"
func main() {
fmt.Print("Enter feet: ")
var input float64
fmt.Scanf("%f", &input)
output := input * 0.3048
fmt.Println(output, "meters")
}