Initial commit
This commit is contained in:
28
06-variables/04-assignment/exercises/01/main.go
Normal file
28
06-variables/04-assignment/exercises/01/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Change `color` variable's value to "blue"
|
||||
//
|
||||
// 2- Print it
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// blue
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DON'T TOUCH THIS:
|
||||
|
||||
// color := "green"
|
||||
|
||||
// ADD YOUR CODE BELOW:
|
||||
|
||||
// ?
|
||||
}
|
18
06-variables/04-assignment/exercises/01/solution/main.go
Normal file
18
06-variables/04-assignment/exercises/01/solution/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
color := "green"
|
||||
|
||||
color = "blue"
|
||||
|
||||
fmt.Println(color)
|
||||
}
|
47
06-variables/04-assignment/exercises/02/main.go
Normal file
47
06-variables/04-assignment/exercises/02/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Change the value of `color` variable to "dark green"
|
||||
//
|
||||
// 2- Do not assign "dark green" to `color` directly
|
||||
//
|
||||
// Instead, use the `color` variable again
|
||||
// while assigning to `color`
|
||||
//
|
||||
// 3- Print it
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// WRONG ANSWER, DO NOT DO THIS:
|
||||
// `color = "dark green"`
|
||||
//
|
||||
// HINT
|
||||
// + operator can concatenate string values
|
||||
//
|
||||
// Example:
|
||||
// "h" + "e" + "y" returns "hey"
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// dark green
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DON'T TOUCH THIS
|
||||
|
||||
// color := "green"
|
||||
|
||||
// ADD YOUR CODE BELOW
|
||||
|
||||
// ?
|
||||
|
||||
// UNCOMMENT THE CODE BELOW TO PRINT THE VARIABLE
|
||||
|
||||
// fmt.Println(color)
|
||||
}
|
20
06-variables/04-assignment/exercises/02/solution/main.go
Normal file
20
06-variables/04-assignment/exercises/02/solution/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
color := "green"
|
||||
|
||||
// `"dark " + color` is an expression
|
||||
|
||||
color = "dark " + color
|
||||
|
||||
fmt.Println(color)
|
||||
}
|
38
06-variables/04-assignment/exercises/03/main.go
Normal file
38
06-variables/04-assignment/exercises/03/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Multiply 3.14 with 2 and assign it to `n` variable
|
||||
//
|
||||
// 2- Print the `n` variable
|
||||
//
|
||||
// HINT
|
||||
// Example: 3 * 2 = 6
|
||||
// * is the product operator (it multiplies numbers)
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 6.28
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DON'T TOUCH THIS
|
||||
|
||||
// Declares a new float64 variable
|
||||
// 0. means 0.0
|
||||
n := 0.
|
||||
|
||||
// ADD YOUR CODE BELOW
|
||||
|
||||
// ?
|
||||
|
||||
fmt.Println(n)
|
||||
}
|
18
06-variables/04-assignment/exercises/03/solution/main.go
Normal file
18
06-variables/04-assignment/exercises/03/solution/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
n := 0.
|
||||
|
||||
n = 3.14 * 2
|
||||
|
||||
fmt.Println(n)
|
||||
}
|
38
06-variables/04-assignment/exercises/04/main.go
Normal file
38
06-variables/04-assignment/exercises/04/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Find the length of a rectangle
|
||||
// Its width is 5
|
||||
// Its height is 6
|
||||
//
|
||||
// 2- Assign the result to the `length` variable
|
||||
//
|
||||
// 3- Print the `length` variable
|
||||
//
|
||||
// HINT
|
||||
// Rectangle formula = 2 * (width + height)
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 22
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT CHANGE THIS
|
||||
|
||||
// var (
|
||||
// length int
|
||||
// width, height = 5, 6
|
||||
// )
|
||||
|
||||
// USE THE VARIABLES ABOVE WHEN CALCULATING YOUR RESULT
|
||||
|
||||
// ADD YOUR CODE BELOW
|
||||
}
|
26
06-variables/04-assignment/exercises/04/solution/main.go
Normal file
26
06-variables/04-assignment/exercises/04/solution/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
length int
|
||||
width, height = 5, 6
|
||||
)
|
||||
|
||||
// first calculates: (width + height)
|
||||
// then : multiplies it with 2
|
||||
|
||||
// just like in math
|
||||
|
||||
length = 2 * (width + height)
|
||||
|
||||
fmt.Println(length)
|
||||
}
|
35
06-variables/04-assignment/exercises/05/main.go
Normal file
35
06-variables/04-assignment/exercises/05/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Assign "go" to `lang` variable
|
||||
// and assign 2 to `version` variable
|
||||
// using a multiple assignment statement
|
||||
//
|
||||
// 2- Print the variables
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go version 2
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT TOUCH THIS
|
||||
var (
|
||||
lang string
|
||||
version int
|
||||
)
|
||||
|
||||
// ADD YOUR CODE BELOW
|
||||
|
||||
// DO NOT TOUCH THIS
|
||||
fmt.Println(lang, "version", version)
|
||||
}
|
21
06-variables/04-assignment/exercises/05/solution/main.go
Normal file
21
06-variables/04-assignment/exercises/05/solution/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
lang string
|
||||
version int
|
||||
)
|
||||
|
||||
lang, version = "go", 2
|
||||
|
||||
fmt.Println(lang, "version", version)
|
||||
}
|
36
06-variables/04-assignment/exercises/06/main.go
Normal file
36
06-variables/04-assignment/exercises/06/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Assign the correct values to the variables
|
||||
// to match to the EXPECTED OUTPUT below
|
||||
//
|
||||
// 2- Print the variables
|
||||
//
|
||||
// HINT
|
||||
// Use multiple Println calls to print each sentence.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Air is good on Mars
|
||||
// It's true
|
||||
// It is 19.5 degrees
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// DO NOT TOUCH THIS
|
||||
|
||||
// var (
|
||||
// planet string
|
||||
// isTrue bool
|
||||
// temp float64
|
||||
// )
|
||||
|
||||
// ADD YOUR CODE BELOW
|
||||
}
|
24
06-variables/04-assignment/exercises/06/solution/main.go
Normal file
24
06-variables/04-assignment/exercises/06/solution/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
planet string
|
||||
isTrue bool
|
||||
temp float64
|
||||
)
|
||||
|
||||
planet, isTrue, temp = "Mars", true, 19.5
|
||||
|
||||
fmt.Println("Air is good on", planet)
|
||||
fmt.Println("It's", isTrue)
|
||||
fmt.Println("It is", temp, "degrees")
|
||||
}
|
40
06-variables/04-assignment/exercises/07/main.go
Normal file
40
06-variables/04-assignment/exercises/07/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Multiple short declare two variables
|
||||
//
|
||||
// 2- Initialize variables using `multi` function below
|
||||
//
|
||||
// 3- Discard the 1st variable's value in the declaration
|
||||
//
|
||||
// 4- Print only the 2nd variable
|
||||
//
|
||||
// NOTE
|
||||
// You should use `multi` function
|
||||
// while initializing the variables
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 4
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(b)
|
||||
}
|
||||
|
||||
// multi is a function that returns multiple int values
|
||||
func multi() (int, int) {
|
||||
return 5, 4
|
||||
}
|
22
06-variables/04-assignment/exercises/07/solution/main.go
Normal file
22
06-variables/04-assignment/exercises/07/solution/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, b := multi()
|
||||
|
||||
fmt.Println(b)
|
||||
}
|
||||
|
||||
func multi() (int, int) {
|
||||
return 5, 4
|
||||
}
|
27
06-variables/04-assignment/exercises/08/main.go
Normal file
27
06-variables/04-assignment/exercises/08/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Change `color` to "orange"
|
||||
// and `color2` to "green" at the same time
|
||||
//
|
||||
// (use multiple-assignment)
|
||||
//
|
||||
// 2- Print the variables
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// orange green
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// color, color2 := "red", "blue"
|
||||
|
||||
// ?
|
||||
}
|
18
06-variables/04-assignment/exercises/08/solution/main.go
Normal file
18
06-variables/04-assignment/exercises/08/solution/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
color, color2 := "red", "blue"
|
||||
|
||||
color, color2 = "orange", "green"
|
||||
|
||||
fmt.Println(color, color2)
|
||||
}
|
23
06-variables/04-assignment/exercises/09/main.go
Normal file
23
06-variables/04-assignment/exercises/09/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Swap the values of `red` and `blue` variables
|
||||
//
|
||||
// 2- Print them
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// blue red
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// red, blue := "red", "blue"
|
||||
// ?
|
||||
}
|
18
06-variables/04-assignment/exercises/09/solution/main.go
Normal file
18
06-variables/04-assignment/exercises/09/solution/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
red, blue := "red", "blue"
|
||||
|
||||
red, blue = blue, red
|
||||
|
||||
fmt.Println(red, blue)
|
||||
}
|
25
06-variables/04-assignment/exercises/10/main.go
Normal file
25
06-variables/04-assignment/exercises/10/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Print only the directory using `path.Split`
|
||||
//
|
||||
// Discard the file part
|
||||
//
|
||||
// RESTRICTION
|
||||
// Use short declaration
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// secret/
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ? ?= path.Split("secret/file.txt")
|
||||
}
|
19
06-variables/04-assignment/exercises/10/solution/main.go
Normal file
19
06-variables/04-assignment/exercises/10/solution/main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dir, _ := path.Split("secret/file.txt")
|
||||
|
||||
fmt.Println(dir)
|
||||
}
|
Reference in New Issue
Block a user