From 873196bd02f30cc578a5f488a0174bfc9c871011 Mon Sep 17 00:00:00 2001 From: sebastian Date: Mon, 2 Oct 2023 13:35:05 +0200 Subject: [PATCH] Section5.46 --- Section5/45.go | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ Section5/46.go | 70 ++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 Section5/45.go create mode 100644 Section5/46.go diff --git a/Section5/45.go b/Section5/45.go new file mode 100644 index 0000000..74d016b --- /dev/null +++ b/Section5/45.go @@ -0,0 +1,121 @@ +/* +Coding Exercise #1 + + Using the var keyword, declare 4 variables called a, b, c, d of type int, float64, bool and string. + + Using short declaration syntax declare and assign these values to variables x, y and z: + + - 20 + + - 15.5 + + - "Gopher!" + + Using fmt.Println() print out the values of a, b, c, d, x, y and z. + + Try to run the program without error. + +package main + +import "fmt" + +func main() { + var a int + var b float64 + var c bool + var d string + x := 20 + y := 15.5 + z := "Gopher!" + + fmt.Println(a, b, c, d) + fmt.Println("X is", x, "\nY is", y, "\nZ is", z) +} +*/ + +/* +Coding Exercise #2 + +Change the code from the previous exercise in the following way: + +1. Declare a, b, c, d using a single var keyword (multiple variable declaration) for better readability. + +2. Declare x, y and z on a single line -> multiple short declarations + +3. Remove the statement that prints out the variables. See the error! + +4. Change the program to run without error using the blank identifier (_) + +Are you stuck? Do you want to see the solution for this exercise? Click here. + +package main + +func main() { + var ( + a int + b float64 + c bool + d string + ) + + x, y, z := 20, 15.5, "Gopher!" + + _, _, _, _, _, _, _ = a, b, c, d, x, y, z +} +*/ + +/* +There are some errors in the following Go program. +Try to identify the errors, change the code and run the program without errors. + + package main + + func main() { + var a float64 = 7.1 + + x, y := true, 3.7 + + a, x := 5.5, false + + _, _, _ = a, x, y + } + + +package main + +func main() { + var a float64 = 7.1 + + x, y := true, 3.7 + + a, x = 5.5, false + + _, _, _ = a, x, y +} +*/ + +/* +Coding Exercise #4 + +There are some errors in the following Go program. Try to identify the errors, change the code and run the program without errors. + + package main + + version := "3.1" + + func main() { + name := 'Golang' + fmt.Println(name) + } +*/ + +package main + +import "fmt" + +var version = "3.1" + +func main() { + name := "Golang" + fmt.Println(name) +} diff --git a/Section5/46.go b/Section5/46.go new file mode 100644 index 0000000..7963a75 --- /dev/null +++ b/Section5/46.go @@ -0,0 +1,70 @@ +/* +Coding Exercise #1 + +Using the const keyword declare and initialize the following constants: + +1. daysWeek with value 7 + +2. lightSpeed with value 299792458 + +3. pi with value 3.14159 + +Run the program without errors. + +package main + +func main() { + const daysWeek = 7 + const lightSpeed = 299792458 + const pi = 3.14159 + +} +*/ + +/* +Coding Exercise #2 + +Change the code from the previous exercise and declare all 3 constants as grouped constants. + +Make them untyped. + +func main() { + const ( + daysWeek = 7 + lightSpeed = 299792458 + pi = 3.14159 + ) + +} +*/ + +/* +Coding Exercise #3 + + Calculate how many seconds are in a year. + + STEPS: + + 1. Declare secPerDay constant and initialize it to the number of seconds in a day + + 2. Declare daysYear constant and initialize it to 365 + + 3. Use fmt.Printf() to print out the total number of seconds in a year. + +EXPECTED OUTPUT: + +There are 31536000 seconds in a year. +*/ +package main + +import "fmt" + +func main() { + const ( + daysYear = 365 + hoursDay = 24 + secondsHour = 3600 + ) + var result = daysYear * hoursDay * secondsHour + fmt.Printf("Seconds in a Year %d\n", result) +}