Initial commit
This commit is contained in:
@ -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 speed int
|
||||
fmt.Println(speed)
|
||||
|
||||
speed = 100
|
||||
fmt.Println(speed)
|
||||
|
||||
speed = speed - 25
|
||||
fmt.Println(speed)
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
// 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
|
||||
|
||||
// Go is a strongly typed programming language.
|
||||
|
||||
// Even a float and an integer are different types.
|
||||
// Or: int32 and int are different types.
|
||||
|
||||
// EXERCISE: Try uncommenting the lines
|
||||
// And observe the errors
|
||||
|
||||
func main() {
|
||||
var speed int
|
||||
// speed = "100"
|
||||
|
||||
var running bool
|
||||
// running = 1
|
||||
|
||||
var force float64
|
||||
// speed = force
|
||||
|
||||
// Go automatically converts the typeless
|
||||
// integer literal to float64 automatically
|
||||
force = 1
|
||||
|
||||
// keep the compiler happy
|
||||
_, _, _ = speed, running, force
|
||||
}
|
50
06-variables/04-assignment/01-assignment/03-examples/main.go
Normal file
50
06-variables/04-assignment/01-assignment/03-examples/main.go
Normal file
@ -0,0 +1,50 @@
|
||||
// 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 (
|
||||
name string
|
||||
age int
|
||||
famous bool
|
||||
)
|
||||
|
||||
// Example #1
|
||||
name = "Newton"
|
||||
age = 84
|
||||
famous = true
|
||||
|
||||
fmt.Println(name, age, famous)
|
||||
|
||||
// Example #2
|
||||
name = "Somebody"
|
||||
age = 20
|
||||
famous = false
|
||||
|
||||
fmt.Println(name, age, famous)
|
||||
|
||||
// Example #3
|
||||
// EXERCISE: Why this doesn't work? Think about it.
|
||||
|
||||
// name = 20
|
||||
// name = age
|
||||
|
||||
// save the previous value of the variable
|
||||
// to a new variable
|
||||
var prevName string
|
||||
prevName = name
|
||||
|
||||
// overwrite the value of the original variable
|
||||
// by assigning to it
|
||||
name = "Einstein"
|
||||
|
||||
fmt.Println("previous name:", prevName)
|
||||
fmt.Println("current name :", name)
|
||||
}
|
26
06-variables/04-assignment/01-overview/main.go
Normal file
26
06-variables/04-assignment/01-overview/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 counter int
|
||||
|
||||
fmt.Println("counter's name : counter")
|
||||
fmt.Println("counter's value:", counter)
|
||||
fmt.Printf("counter's type : %T\n", counter)
|
||||
|
||||
counter = 10 // OK
|
||||
// counter = "ten" // NOT OK
|
||||
|
||||
fmt.Println("counter's value:", counter)
|
||||
|
||||
counter++
|
||||
fmt.Println("counter's value:", counter)
|
||||
}
|
29
06-variables/04-assignment/05-multiple-assignment/main.go
Normal file
29
06-variables/04-assignment/05-multiple-assignment/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
// 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"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
speed int
|
||||
now time.Time
|
||||
)
|
||||
|
||||
speed, now = 100, time.Now()
|
||||
|
||||
fmt.Println(speed, now)
|
||||
|
||||
// EXERCISE:
|
||||
// Try this alternative (formatted time).
|
||||
|
||||
// fmt.Println(speed, now.Format(time.Kitchen))
|
||||
}
|
21
06-variables/04-assignment/06-swapping/main.go
Normal file
21
06-variables/04-assignment/06-swapping/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 (
|
||||
speed = 100
|
||||
prevSpeed = 50
|
||||
)
|
||||
|
||||
speed, prevSpeed = prevSpeed, speed
|
||||
|
||||
fmt.Println(speed, prevSpeed)
|
||||
}
|
22
06-variables/04-assignment/07-path-project/main.go
Normal file
22
06-variables/04-assignment/07-path-project/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"
|
||||
"path"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var dir, file string
|
||||
|
||||
dir, file = path.Split("css/main.css")
|
||||
|
||||
fmt.Println("dir :", dir)
|
||||
fmt.Println("file:", 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"
|
||||
"path"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var file string
|
||||
|
||||
_, file = path.Split("css/main.css")
|
||||
|
||||
fmt.Println("file:", file)
|
||||
}
|
22
06-variables/04-assignment/09-path-project-shortdecl/main.go
Normal file
22
06-variables/04-assignment/09-path-project-shortdecl/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"
|
||||
"path"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, file := path.Split("css/main.css")
|
||||
|
||||
// or this:
|
||||
// dir, file := path.Split("css/main.css")
|
||||
|
||||
fmt.Println("file:", file)
|
||||
}
|
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)
|
||||
}
|
66
06-variables/04-assignment/questions/questions.md
Normal file
66
06-variables/04-assignment/questions/questions.md
Normal file
@ -0,0 +1,66 @@
|
||||
## What happens when you assign a variable to another variable?
|
||||
* The variables become the same variable
|
||||
* Assignee variable gets deleted
|
||||
* Variable's value is changed to the assigned variable's value *CORRECT*
|
||||
|
||||
## Which one is a correct assignment statement?
|
||||
```go
|
||||
opened := true
|
||||
```
|
||||
* `closed := true`
|
||||
* `opened = false` *CORRECT*
|
||||
* `var x = 2`
|
||||
|
||||
## Which one is a correct assignment statement?
|
||||
* `a, b = 3; 5`
|
||||
* `c, d = true, false` *CORRECT*
|
||||
* `a, b, c = 5, 3`
|
||||
|
||||
## Which one is a correct assignment?
|
||||
```go
|
||||
var (
|
||||
n = 3
|
||||
m int
|
||||
)
|
||||
```
|
||||
|
||||
* `m = "4"`
|
||||
* `n = 10` *CORRECT*
|
||||
* `n = true`
|
||||
* `m = false`
|
||||
|
||||
## Which one is a correct assignment?
|
||||
```go
|
||||
var (
|
||||
n = 3
|
||||
m int
|
||||
f float64
|
||||
)
|
||||
```
|
||||
|
||||
* `n, m = 4, f`
|
||||
* `n = false`
|
||||
* `n, m = m + n, n + 5.0` *CORRECT*
|
||||
* `n, m = 3.82, "hi"`
|
||||
|
||||
## Which one is a correct assignment statement?
|
||||
```go
|
||||
var (
|
||||
a int
|
||||
c bool
|
||||
)
|
||||
```
|
||||
* `a = _`
|
||||
* `c, _ = _, false` *CORRECT*
|
||||
* `_, _ = true, true`
|
||||
|
||||
## Which one is a correct assignment?
|
||||
**REMEMBER:** `path.Split` returns two `string` values
|
||||
|
||||
```go
|
||||
var c, f string
|
||||
```
|
||||
* `_ = path.Split("assets/profile.png")`
|
||||
* `_, _, c = path.Split("assets/profile.png")`
|
||||
* `f = path.Split("assets/profile.png")`
|
||||
* `_, f = path.Split("assets/profile.png")` *CORRECT*
|
Reference in New Issue
Block a user