Initial commit
This commit is contained in:
21
06-variables/01-basic-data-types/exercises/01/main.go
Normal file
21
06-variables/01-basic-data-types/exercises/01/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Print a few integer literals
|
||||
// 2- Print a few float literals
|
||||
// 3- Print true and false bool literals
|
||||
// 4- Print your name using a string literal
|
||||
// 5- Print a non-english sentence using a string literal
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// Use fmt.Println()
|
||||
}
|
||||
@@ -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() {
|
||||
fmt.Println(42, 8500, 344433, -2323)
|
||||
fmt.Println(3.14, 6.28, -42.)
|
||||
fmt.Println(true, false)
|
||||
fmt.Println("Hi! I'm Inanc!")
|
||||
fmt.Println("Merhaba, adım İnanç!")
|
||||
}
|
||||
61
06-variables/01-basic-data-types/exercises/02/main.go
Normal file
61
06-variables/01-basic-data-types/exercises/02/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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"
|
||||
|
||||
// THIS EXERCISE IS OPTIONAL
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Print 0 to 9 in hexadecimal
|
||||
// 2- Print 10 to 15 in hexadecimal
|
||||
// 3- Print 17 in hexadecimal
|
||||
// 4- Print 25 in hexadecimal
|
||||
// 5- Print 50 in hexadecimal
|
||||
// 6- Print 100 in hexadecimal
|
||||
//
|
||||
// NOTES
|
||||
// https://stackoverflow.com/questions/910309/how-to-turn-hexadecimal-into-decimal-using-brain
|
||||
//
|
||||
// https://simple.wikipedia.org/wiki/Hexadecimal_numeral_system
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 0 1 2 3 4 5 6 7 8 9
|
||||
// 10 11 12 13 14 15
|
||||
// 17
|
||||
// 25
|
||||
// 50
|
||||
// 100
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// EXAMPLES:
|
||||
|
||||
// I'm going to print 10 in hexadecimal
|
||||
fmt.Println(0xa)
|
||||
|
||||
// I'm going to print 16 in hexadecimal
|
||||
// 0x10
|
||||
// ^^----- 1 * 0 = 0
|
||||
// |
|
||||
// +------ 16 * 1 = 16
|
||||
// = 16
|
||||
fmt.Println(0x10)
|
||||
|
||||
// I'm going to print 150 in hexadecimal
|
||||
// 0x96
|
||||
// ^^----- 1 * 6 = 6
|
||||
// |
|
||||
// +------ 16 * 9 = 144
|
||||
// = 150
|
||||
fmt.Println(0x96)
|
||||
|
||||
// COMMENT-OUT ALL THE CODE ABOVE, THEN,
|
||||
// ADD YOUR OWN SOLUTIONS BELOW
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
func main() {
|
||||
fmt.Println(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9)
|
||||
fmt.Println(0xa, 0xb, 0xc, 0xd, 0xe, 0xf)
|
||||
fmt.Println(0x11) // 17
|
||||
fmt.Println(0x19) // 25
|
||||
fmt.Println(0x32) // 50
|
||||
fmt.Println(0x64) // 100
|
||||
}
|
||||
37
06-variables/01-basic-data-types/main.go
Normal file
37
06-variables/01-basic-data-types/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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() {
|
||||
// integer literal
|
||||
fmt.Println(
|
||||
-200, -100, 0, 50, 100, 100,
|
||||
)
|
||||
|
||||
// float literal
|
||||
fmt.Println(
|
||||
-50.5, -20.5, -0., 1., 100.56, // ...
|
||||
)
|
||||
|
||||
// bool constants
|
||||
fmt.Println(
|
||||
true, false,
|
||||
)
|
||||
|
||||
// string literal - utf-8
|
||||
fmt.Println(
|
||||
"", // empty - prints just a space
|
||||
"hi",
|
||||
|
||||
// unicode
|
||||
"nasılsın?", // "how are you" in turkish
|
||||
"hi there 星!", // "hi there star!"
|
||||
)
|
||||
}
|
||||
33
06-variables/01-basic-data-types/questions/questions.md
Normal file
33
06-variables/01-basic-data-types/questions/questions.md
Normal file
@@ -0,0 +1,33 @@
|
||||
## Which one below is an integer literal?
|
||||
* -42 *CORRECT*
|
||||
* This is an integer literal
|
||||
* "Hello"
|
||||
* This is a string literal
|
||||
* false
|
||||
* This is a bool constant
|
||||
* 3.14
|
||||
* This is a float literal
|
||||
|
||||
## Which one below is a float literal?
|
||||
* -42
|
||||
* "Hello"
|
||||
* false
|
||||
* 3.14 *CORRECT*
|
||||
|
||||
## Which one below is a float literal?
|
||||
* 6,28
|
||||
* ,28
|
||||
* .28 *CORRECT*
|
||||
* 628
|
||||
|
||||
## Which one below is a string literal?
|
||||
* -42
|
||||
* "Hello" *CORRECT*
|
||||
* false
|
||||
* 3.14
|
||||
|
||||
## Which one below is a bool constant?
|
||||
* -42
|
||||
* "Hello"
|
||||
* false *CORRECT*
|
||||
* 3.14
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
// VARIABLE NAMING RULES
|
||||
|
||||
func main() {
|
||||
// CORRECT DECLARATIONS
|
||||
var speed int
|
||||
var SpeeD int
|
||||
|
||||
// underscore is allowed but not recommended
|
||||
var _speed int
|
||||
|
||||
// unicode letters are allowed
|
||||
var 速度 int
|
||||
|
||||
// keep the compiler happy
|
||||
_, _, _, _ = speed, SpeeD, _speed, 速度
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
// fmt.Println(speed)
|
||||
// var speed int
|
||||
}
|
||||
@@ -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() {
|
||||
var nFiles int
|
||||
var counter int
|
||||
var nCPU int
|
||||
|
||||
fmt.Println(
|
||||
nFiles,
|
||||
counter,
|
||||
nCPU,
|
||||
)
|
||||
}
|
||||
@@ -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() {
|
||||
var heat float64
|
||||
var ratio float64
|
||||
var degree float64
|
||||
|
||||
fmt.Println(
|
||||
heat,
|
||||
ratio,
|
||||
degree,
|
||||
)
|
||||
}
|
||||
@@ -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() {
|
||||
var off bool
|
||||
var valid bool
|
||||
var closed bool
|
||||
|
||||
fmt.Println(
|
||||
off,
|
||||
valid,
|
||||
closed,
|
||||
)
|
||||
}
|
||||
@@ -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() {
|
||||
var msg string
|
||||
var name string
|
||||
var text string
|
||||
|
||||
fmt.Println(
|
||||
msg,
|
||||
name,
|
||||
text,
|
||||
)
|
||||
}
|
||||
27
06-variables/02-declarations/03-zero-values/main.go
Normal file
27
06-variables/02-declarations/03-zero-values/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
|
||||
|
||||
import "fmt"
|
||||
|
||||
// EXERCISE: Let's run this to see the zero-values yourself
|
||||
|
||||
func main() {
|
||||
var speed int // numeric type
|
||||
var heat float64 // numeric type
|
||||
var off bool
|
||||
var brand string
|
||||
|
||||
fmt.Println(speed)
|
||||
fmt.Println(heat)
|
||||
fmt.Println(off)
|
||||
|
||||
// I've used printf to print an empty string
|
||||
// EXERCISE: Use Println to see what happens
|
||||
fmt.Printf("%q\n", brand)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
// there's no warning for package-level vars
|
||||
var packageLevelVar string
|
||||
|
||||
func main() {
|
||||
// unused variable error
|
||||
// var speed int
|
||||
|
||||
// if you use it, the error will be gone
|
||||
// fmt.Println(speed)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
var speed int
|
||||
|
||||
// let's assign the variable to the blank-identifier
|
||||
// so that Go compiler won't get grumpy
|
||||
_ = speed
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
speed int
|
||||
heat float64
|
||||
|
||||
off bool
|
||||
brand string
|
||||
)
|
||||
|
||||
fmt.Println(speed)
|
||||
fmt.Println(heat)
|
||||
fmt.Println(off)
|
||||
fmt.Printf("%q\n", brand)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// this is equal to:
|
||||
//
|
||||
// var (
|
||||
// speed int
|
||||
// velocity int
|
||||
// )
|
||||
//
|
||||
// or:
|
||||
//
|
||||
// var speed int
|
||||
// var velocity int
|
||||
//
|
||||
var speed, velocity int
|
||||
|
||||
fmt.Println(speed, velocity)
|
||||
}
|
||||
41
06-variables/02-declarations/06-examples/main.go
Normal file
41
06-variables/02-declarations/06-examples/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// 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() {
|
||||
// names are case-sensitive:
|
||||
// MyAge, myAge, and MYAGE are different variables
|
||||
|
||||
// USE-CASE:
|
||||
// When to use a parallel declaration?
|
||||
//
|
||||
// NOT GOOD:
|
||||
// var myAge int
|
||||
// var yourAge int
|
||||
//
|
||||
// SO-SO:
|
||||
// var (
|
||||
// myAge int
|
||||
// yourAge int
|
||||
// )
|
||||
//
|
||||
// BETTER:
|
||||
var myAge, yourAge int
|
||||
fmt.Println(myAge, yourAge)
|
||||
|
||||
var temperature float64
|
||||
fmt.Println(temperature)
|
||||
|
||||
var success bool
|
||||
fmt.Println(success)
|
||||
|
||||
var language string
|
||||
fmt.Println(language)
|
||||
}
|
||||
22
06-variables/02-declarations/exercises/01/main.go
Normal file
22
06-variables/02-declarations/exercises/01/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Declare and print a variable with an int type
|
||||
// 2- The declared variable's name should be: height
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 0
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var ? ?
|
||||
// ?
|
||||
}
|
||||
18
06-variables/02-declarations/exercises/01/solution/main.go
Normal file
18
06-variables/02-declarations/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() {
|
||||
var height int
|
||||
|
||||
fmt.Println(height)
|
||||
}
|
||||
22
06-variables/02-declarations/exercises/02/main.go
Normal file
22
06-variables/02-declarations/exercises/02/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// OPTIONAL EXERCISE
|
||||
// 1- Declare and print a bool variable
|
||||
// 2- The variable's name should be: isOn
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// false
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var ? ?
|
||||
// ?
|
||||
}
|
||||
17
06-variables/02-declarations/exercises/02/solution/main.go
Normal file
17
06-variables/02-declarations/exercises/02/solution/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// 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 isOn bool
|
||||
fmt.Println(isOn)
|
||||
}
|
||||
22
06-variables/02-declarations/exercises/03/main.go
Normal file
22
06-variables/02-declarations/exercises/03/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Declare and print a variable with a float64 type
|
||||
// 2- The declared variable's name should be: brightness
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 0
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var ? ?
|
||||
// ?
|
||||
}
|
||||
18
06-variables/02-declarations/exercises/03/solution/main.go
Normal file
18
06-variables/02-declarations/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() {
|
||||
var brightness int
|
||||
|
||||
fmt.Println(brightness)
|
||||
}
|
||||
28
06-variables/02-declarations/exercises/04/main.go
Normal file
28
06-variables/02-declarations/exercises/04/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- Declare a string variable
|
||||
// 2- Print that variable
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// ""
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// USE THE BELOW CODE
|
||||
// You'll learn about Printf later
|
||||
|
||||
// var ?
|
||||
// fmt.Printf("s (%T): %q\n", s, s)
|
||||
|
||||
// %T prints the type of the value
|
||||
// %q prints an empty string
|
||||
}
|
||||
15
06-variables/02-declarations/exercises/04/solution/main.go
Normal file
15
06-variables/02-declarations/exercises/04/solution/main.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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 s string
|
||||
fmt.Printf("s (%T): %q\n", s, s)
|
||||
}
|
||||
33
06-variables/02-declarations/exercises/05/main.go
Normal file
33
06-variables/02-declarations/exercises/05/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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- Declare the variables below:
|
||||
// 3speed
|
||||
// !speed
|
||||
// spe?ed
|
||||
// var
|
||||
// func
|
||||
// package
|
||||
//
|
||||
// 2- Observe the error messages
|
||||
//
|
||||
// NOTE
|
||||
// The types of the variables are not important.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var ? int
|
||||
// var ? int
|
||||
// var ? int
|
||||
// var ? int
|
||||
// var ? int
|
||||
// var ? int
|
||||
}
|
||||
17
06-variables/02-declarations/exercises/05/solution/main.go
Normal file
17
06-variables/02-declarations/exercises/05/solution/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
// var 3speed int
|
||||
// var !speed int
|
||||
// var spe?ed int
|
||||
// var var int
|
||||
// var func int
|
||||
// var package int
|
||||
}
|
||||
41
06-variables/02-declarations/exercises/06/main.go
Normal file
41
06-variables/02-declarations/exercises/06/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// 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- Declare a few variables using the following types
|
||||
// int
|
||||
// int8
|
||||
// int16
|
||||
// int32
|
||||
// int64
|
||||
// float32
|
||||
// float64
|
||||
// complex64
|
||||
// complex128
|
||||
// bool
|
||||
// string
|
||||
// rune
|
||||
// byte
|
||||
//
|
||||
// 2- Observe their output
|
||||
// 3- After you've done, check out the solution
|
||||
// and read the comments there
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 0 0 0 0 0 0 0 false 0 0
|
||||
// ""
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var i int
|
||||
// var i8 int8
|
||||
|
||||
// CONTINUE FROM HERE....
|
||||
}
|
||||
40
06-variables/02-declarations/exercises/06/solution/main.go
Normal file
40
06-variables/02-declarations/exercises/06/solution/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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// integer types
|
||||
var i int
|
||||
var i8 int8
|
||||
var i16 int16
|
||||
var i32 int32
|
||||
var i64 int64
|
||||
|
||||
// float types
|
||||
var f32 float32
|
||||
var f64 float64
|
||||
|
||||
// bool type
|
||||
var b bool
|
||||
|
||||
// string types
|
||||
var s string
|
||||
var r rune // also a numeric type
|
||||
var by byte // also a numeric type
|
||||
|
||||
fmt.Println(
|
||||
i, i8, i16, i32, i64,
|
||||
f32, f64,
|
||||
b, r, by,
|
||||
)
|
||||
|
||||
// You could do it with Println as well
|
||||
fmt.Printf("%q\n", s)
|
||||
}
|
||||
32
06-variables/02-declarations/exercises/07/main.go
Normal file
32
06-variables/02-declarations/exercises/07/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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. Declare two variables using
|
||||
// multiple variable declaration statement
|
||||
//
|
||||
// 2. The first variable's name should be active
|
||||
// 3. The second variable's name should be delta
|
||||
//
|
||||
// 4. Print them all
|
||||
//
|
||||
// HINT
|
||||
// You should declare a bool and an int variable
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// false 0
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// var (
|
||||
// ?
|
||||
// )
|
||||
// fmt.Println(active, delta)
|
||||
}
|
||||
18
06-variables/02-declarations/exercises/07/solution/main.go
Normal file
18
06-variables/02-declarations/exercises/07/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() {
|
||||
var (
|
||||
active bool
|
||||
delta int
|
||||
)
|
||||
fmt.Println(active, delta)
|
||||
}
|
||||
34
06-variables/02-declarations/exercises/08/main.go
Normal file
34
06-variables/02-declarations/exercises/08/main.go
Normal file
@@ -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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1. Declare and initialize two string variables
|
||||
// using multiple variable declaration
|
||||
//
|
||||
// 2. Use the type once while declaring the variables
|
||||
//
|
||||
// 3. The first variable's name should be firstName
|
||||
// 4. The second variable's name should be lastName
|
||||
//
|
||||
// 5. Print them all
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// "" ""
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATION HERE
|
||||
//
|
||||
|
||||
// REPLACE THE QUESTION-MARKS BELOW
|
||||
// WITH THE NAME OF YOUR VARIABLES
|
||||
|
||||
// fmt.Printf("%q %q\n", ?, ?)
|
||||
}
|
||||
15
06-variables/02-declarations/exercises/08/solution/main.go
Normal file
15
06-variables/02-declarations/exercises/08/solution/main.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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 firstName, lastName string = "", ""
|
||||
fmt.Printf("%q %q\n", firstName, lastName)
|
||||
}
|
||||
21
06-variables/02-declarations/exercises/09/main.go
Normal file
21
06-variables/02-declarations/exercises/09/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Declare a variable
|
||||
// 2- Variable's name should be: isLiquid
|
||||
// 3- Discard it using a blank-identifier
|
||||
//
|
||||
// NOTE
|
||||
// Do not print the variable
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
||||
13
06-variables/02-declarations/exercises/09/solution/main.go
Normal file
13
06-variables/02-declarations/exercises/09/solution/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
var isLiquid bool
|
||||
_ = isLiquid
|
||||
}
|
||||
19
06-variables/02-declarations/exercises/10/main.go
Normal file
19
06-variables/02-declarations/exercises/10/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Declare a variable in the package-scope
|
||||
//
|
||||
// 2- Observe whether something happens when you don't
|
||||
// use it
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
||||
13
06-variables/02-declarations/exercises/10/solution/main.go
Normal file
13
06-variables/02-declarations/exercises/10/solution/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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
|
||||
|
||||
var isLiquid bool
|
||||
|
||||
func main() {
|
||||
}
|
||||
24
06-variables/02-declarations/exercises/11/main.go
Normal file
24
06-variables/02-declarations/exercises/11/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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Print a variable
|
||||
// 2- Then declare it
|
||||
// (This means: Try to print it before its declaration)
|
||||
// 3- Observe the error
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// First print it:
|
||||
// fmt.Println(?)
|
||||
|
||||
// Then declare it:
|
||||
// var ? ?
|
||||
}
|
||||
15
06-variables/02-declarations/exercises/11/solution/main.go
Normal file
15
06-variables/02-declarations/exercises/11/solution/main.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
|
||||
func main() {
|
||||
// UNCOMMENT THE CODE BELOW TO SEE THE ERROR
|
||||
|
||||
// fmt.Println(age)
|
||||
// var age int
|
||||
}
|
||||
22
06-variables/02-declarations/questions/01-what.md
Normal file
22
06-variables/02-declarations/questions/01-what.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# QUESTIONS: What's a variable?
|
||||
|
||||
* **Where does a variable live?**
|
||||
* Hard Disk
|
||||
* Computer Memory - *CORRECT*
|
||||
* CPU
|
||||
|
||||
* **What do you use a variable's name for?**
|
||||
* To be able to access it later - *CORRECT*
|
||||
* It's just a label
|
||||
* I don't need to use it
|
||||
|
||||
* **How to change the value of a variable?**
|
||||
* By using its name - *CORRECT*
|
||||
* By using its value
|
||||
* By asking to Go
|
||||
|
||||
* **After its declaration can you change the variable's type?**
|
||||
* Yes : Go is dynamically-typed
|
||||
* No : Go is statically-typed - *CORRECT*
|
||||
* Both: Go supports both of them
|
||||
|
||||
21
06-variables/02-declarations/questions/02-declaration.md
Normal file
21
06-variables/02-declarations/questions/02-declaration.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## Which statement do you need to use for declaring variables?
|
||||
* name int
|
||||
* vars string name
|
||||
* var name integer
|
||||
* var width int *CORRECT*
|
||||
|
||||
## Which sentence below is correct?
|
||||
* You can use a variable before declaring it
|
||||
* You have to declare a variable before using it *CORRECT*
|
||||
|
||||
## What kind of language is Go?
|
||||
* Weakly-Typed
|
||||
* Dynamically-Typed
|
||||
* Strongly-Typed *CORRECT*
|
||||
* Freely-Typed
|
||||
|
||||
## Which variable name below is correct?
|
||||
* int
|
||||
* four *CORRECT*
|
||||
* 2computers
|
||||
* one?there
|
||||
@@ -0,0 +1,14 @@
|
||||
## What happens when you don't use a declared variable in the block scope?
|
||||
* Nothing, it will work fine
|
||||
* It will get removed automatically
|
||||
* The program won't compile *CORRECT*
|
||||
|
||||
## What happens when you don't use a declared variable in the package scope?
|
||||
* Nothing, it will work fine *CORRECT*
|
||||
* It will get removed automatically
|
||||
* The program won't compile
|
||||
|
||||
## How can you prevent unused variable error?
|
||||
* You can change the variable's name
|
||||
* You can use a blank-identifier to discard it *CORRECT*
|
||||
* You can change the variable's type
|
||||
23
06-variables/02-declarations/questions/04-zero-values.md
Normal file
23
06-variables/02-declarations/questions/04-zero-values.md
Normal file
@@ -0,0 +1,23 @@
|
||||
## Which type's zero value is 0?
|
||||
- bool
|
||||
- pointer
|
||||
- string
|
||||
- all numeric types *CORRECT*
|
||||
|
||||
## Which type's zero value is false?
|
||||
- bool *CORRECT*
|
||||
- pointer
|
||||
- string
|
||||
- all numeric types
|
||||
|
||||
## Which type's zero value is ""?
|
||||
- bool
|
||||
- pointer
|
||||
- string *CORRECT*
|
||||
- all numeric types
|
||||
|
||||
## Which type's zero value is nil?
|
||||
- bool
|
||||
- pointer *CORRECT*
|
||||
- string
|
||||
- all numeric types
|
||||
@@ -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() {
|
||||
// = is the assignment operator
|
||||
// when used within a variable declaration, it
|
||||
// initializes the variable to the given value
|
||||
|
||||
// here, Go initializes the safe variable to true
|
||||
|
||||
// OPTION #1 (option #2 is better)
|
||||
// var safe bool = true
|
||||
|
||||
// OPTION #2
|
||||
var safe = true
|
||||
|
||||
fmt.Println(safe)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// OPTION #1 (option #2 is better)
|
||||
// var safe bool = true
|
||||
|
||||
// OPTION #2 (OK)
|
||||
// var safe = true
|
||||
|
||||
// OPTION #3 - SHORT DECLARATION (BEST)
|
||||
//
|
||||
// You don't even need to type the `var` keyword
|
||||
//
|
||||
// Short declaration equals to:
|
||||
// var safe bool = true
|
||||
// var safe = true
|
||||
//
|
||||
// Go gets (infers) the type from the initializer value
|
||||
//
|
||||
// true's default type is bool
|
||||
// so, the type of the safe variable becomes a bool
|
||||
safe := true
|
||||
|
||||
fmt.Println(safe)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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 = "Carl"
|
||||
// var name = "Carl"
|
||||
name := "Carl"
|
||||
|
||||
// var isScientist bool = true
|
||||
// var isScientist = true
|
||||
isScientist := true
|
||||
|
||||
// var age int = 62
|
||||
// var age = 62
|
||||
age := 62
|
||||
|
||||
// var degree float64 = 5.
|
||||
// var degree = 5.
|
||||
degree := 5.
|
||||
|
||||
fmt.Println(name, isScientist, age, degree)
|
||||
|
||||
// type inference also works for variables
|
||||
//
|
||||
// Go gets the type of the variable and assigns it
|
||||
// to the newly declared variable
|
||||
//
|
||||
// The type of the name2 variable is `string` now
|
||||
name2 := name
|
||||
fmt.Println(name2)
|
||||
}
|
||||
27
06-variables/03-short-declaration/02-package-scope/main.go
Normal file
27
06-variables/03-short-declaration/02-package-scope/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
|
||||
|
||||
import "fmt"
|
||||
|
||||
// You can't use declaration statements without a keyword
|
||||
// Short declaration doesn't have a keyword (`var`)
|
||||
// So, it can't be used at the package scope
|
||||
//
|
||||
// SYNTAX ERROR:
|
||||
// "non-declaration statement outside function body"
|
||||
|
||||
// safe := true
|
||||
|
||||
// However, you can use the normal declaration at the
|
||||
// package scope. Since, it has a keyword: `var`
|
||||
var safe = true
|
||||
|
||||
func main() {
|
||||
fmt.Println(safe)
|
||||
}
|
||||
@@ -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() {
|
||||
// the number of variables and values should be equal
|
||||
|
||||
// -> `true` is being assigned to `safe`
|
||||
// -> `50` is being assigned to `speed`
|
||||
|
||||
safe, speed := true, 50
|
||||
|
||||
fmt.Println(safe, speed)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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() {
|
||||
name, lastname := "Nikola", "Tesla"
|
||||
fmt.Println(name, lastname)
|
||||
|
||||
birth, death := 1856, 1943
|
||||
fmt.Println(birth, death)
|
||||
|
||||
on, off := true, false
|
||||
fmt.Println(on, off)
|
||||
|
||||
// there's no limit
|
||||
// however, more declarations that you declare
|
||||
// more unreadable it becomes...
|
||||
degree, ratio, heat := 10.55, 30.5, 20.
|
||||
fmt.Println(degree, ratio, heat)
|
||||
|
||||
// you can short declare variables with different types
|
||||
nFiles, valid, msg := 10, true, "hello"
|
||||
|
||||
fmt.Println(nFiles, valid, msg)
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
func main() {
|
||||
// `safe`'s value is `false`
|
||||
var safe bool
|
||||
|
||||
// `safe`'s value is now `true`
|
||||
|
||||
// `speed` is declared and initialized to `50`
|
||||
|
||||
// redeclaration only works when
|
||||
//
|
||||
// at least one of the variables
|
||||
// should be a new variable
|
||||
|
||||
safe, speed := true, 50
|
||||
|
||||
fmt.Println(safe, speed)
|
||||
|
||||
// EXERCISE
|
||||
//
|
||||
// Declare the speed variable before
|
||||
// the short declaration "again"
|
||||
//
|
||||
// Observe what happens
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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() {
|
||||
// EXAMPLE #1
|
||||
|
||||
name := "Nikola"
|
||||
fmt.Println(name)
|
||||
|
||||
// name already exists in this block
|
||||
// name := "Marie"
|
||||
|
||||
// just assigns new values to name
|
||||
// and declares the new variable age with a value of 66
|
||||
name, age := "Marie", 66
|
||||
fmt.Println(name, age)
|
||||
|
||||
// EXAMPLE #2
|
||||
|
||||
// name = "Albert"
|
||||
// birth := 1879
|
||||
|
||||
// redeclaration below equals to the statements just above
|
||||
//
|
||||
// `name` is an existing variable
|
||||
// Go just assigns "Albert" to the name variable
|
||||
//
|
||||
// `birth` is a new variable
|
||||
// Go declares it and assigns it a value of `1879`
|
||||
name, birth := "Albert", 1879
|
||||
|
||||
fmt.Println(name, birth)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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
|
||||
|
||||
// normal declaration use cases
|
||||
|
||||
// -----------------------------------------------------
|
||||
// when you need a package scoped variable
|
||||
// -----------------------------------------------------
|
||||
|
||||
// version := 0 // YOU CAN'T
|
||||
var version int
|
||||
|
||||
func main() {
|
||||
|
||||
// -----------------------------------------------------
|
||||
// if you don't know the initial value
|
||||
// -----------------------------------------------------
|
||||
|
||||
// DON'T DO THIS:
|
||||
// score := 0
|
||||
|
||||
// DO THIS:
|
||||
// var score int
|
||||
|
||||
// -----------------------------------------------------
|
||||
// group variables for readability
|
||||
// -----------------------------------------------------
|
||||
|
||||
// var (
|
||||
// video string
|
||||
|
||||
// duration int
|
||||
// current int
|
||||
// )
|
||||
}
|
||||
@@ -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
|
||||
|
||||
// short declaration use cases
|
||||
|
||||
func main() {
|
||||
|
||||
// -----------------------------------------------------
|
||||
// if you know the initial value
|
||||
// -----------------------------------------------------
|
||||
|
||||
// DON'T DO THIS:
|
||||
// var width, height = 100, 50
|
||||
|
||||
// DO THIS (concise):
|
||||
// width, height := 100, 50
|
||||
|
||||
// -----------------------------------------------------
|
||||
// redeclaration
|
||||
// -----------------------------------------------------
|
||||
|
||||
// DON'T DO THIS:
|
||||
// width = 50
|
||||
// color := red
|
||||
|
||||
// DO THIS (concise):
|
||||
// width, color := 50, "red"
|
||||
}
|
||||
31
06-variables/03-short-declaration/exercises/01/main.go
Normal file
31
06-variables/03-short-declaration/exercises/01/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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
|
||||
// Declare and then print four variables
|
||||
// using the short declaration statement
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// i: 314 f: 3.14 s: Hello b: true
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(
|
||||
// "i:", i,
|
||||
// "f:", f,
|
||||
// "s:", s,
|
||||
// "b:", b,
|
||||
// )
|
||||
}
|
||||
@@ -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() {
|
||||
i := 314
|
||||
f := 3.14
|
||||
s := "Hello"
|
||||
b := true
|
||||
|
||||
fmt.Println(
|
||||
"i:", i,
|
||||
"f:", f,
|
||||
"s:", s,
|
||||
"b:", b,
|
||||
)
|
||||
}
|
||||
25
06-variables/03-short-declaration/exercises/02/main.go
Normal file
25
06-variables/03-short-declaration/exercises/02/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
|
||||
// Declare two variables using multiple short declaration
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 14 true
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(a, b)
|
||||
}
|
||||
@@ -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() {
|
||||
a, b := 14, true
|
||||
|
||||
fmt.Println(a, b)
|
||||
}
|
||||
28
06-variables/03-short-declaration/exercises/03/main.go
Normal file
28
06-variables/03-short-declaration/exercises/03/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- Declare two variables using short declaration
|
||||
//
|
||||
// 2- `a` variable's value should be 42
|
||||
// 3- `c` variable's value should be "good"
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 42 good
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(a, c)
|
||||
}
|
||||
@@ -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() {
|
||||
a, c := 42, "good"
|
||||
|
||||
fmt.Println(a, c)
|
||||
}
|
||||
25
06-variables/03-short-declaration/exercises/04/main.go
Normal file
25
06-variables/03-short-declaration/exercises/04/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
|
||||
// 1- Short declare a variable named `sum`
|
||||
// 2- Initialize it with an expression by adding 27 and 3.5
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 30.5
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATION HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
// fmt.Println(sum)
|
||||
}
|
||||
@@ -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() {
|
||||
sum := 27 + 3.5
|
||||
|
||||
fmt.Println(sum)
|
||||
}
|
||||
34
06-variables/03-short-declaration/exercises/05/main.go
Normal file
34
06-variables/03-short-declaration/exercises/05/main.go
Normal file
@@ -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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// 1- Short declare two bool variables
|
||||
// (use multiple short declaration syntax)
|
||||
//
|
||||
// 2- Initialize both variables to true
|
||||
//
|
||||
// 3- Change your declaration and
|
||||
// discard the 2nd variable's value
|
||||
// using the blank-identifier
|
||||
//
|
||||
// 4- Print only the 1st variable
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// true
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(on)
|
||||
}
|
||||
@@ -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() {
|
||||
// You can discard values in a short declaration
|
||||
|
||||
on, _ := true, true
|
||||
|
||||
fmt.Println(on)
|
||||
}
|
||||
33
06-variables/03-short-declaration/exercises/06/main.go
Normal file
33
06-variables/03-short-declaration/exercises/06/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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- Short declare two int variables: age and yourAge
|
||||
// (use multiple short declaration syntax)
|
||||
//
|
||||
// 2- Short declare a new float variable: ratio
|
||||
// And, change the 'age' variable to 42
|
||||
//
|
||||
// (! You should use redeclaration)
|
||||
//
|
||||
// 4- Print all the variables
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// 42, 20, 3.14
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ADD YOUR DECLARATIONS HERE
|
||||
//
|
||||
|
||||
// THEN UNCOMMENT THE CODE BELOW
|
||||
|
||||
// fmt.Println(age, yourAge, ratio)
|
||||
}
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
age, yourAge := 10, 20
|
||||
age, ratio := 42, 3.14
|
||||
|
||||
fmt.Println(age, yourAge, ratio)
|
||||
}
|
||||
101
06-variables/03-short-declaration/questions/questions.md
Normal file
101
06-variables/03-short-declaration/questions/questions.md
Normal file
@@ -0,0 +1,101 @@
|
||||
## Which is a correct declaration?
|
||||
* var int safe := 3
|
||||
* var safe bool := 3
|
||||
* safe := true *CORRECT*
|
||||
|
||||
## Which is a correct declaration?
|
||||
* var short := true
|
||||
* int num := 1
|
||||
* speed := 50 *CORRECT*
|
||||
* num int := 2
|
||||
|
||||
## Which is a correct declaration?
|
||||
* x, y, z := 10, 20
|
||||
* x = 10,
|
||||
* y, x, p := 5, "hi", 1.5 *CORRECT*
|
||||
* y, x = "hello", 10
|
||||
|
||||
## Which declaration is equal to the following declaration?
|
||||
```go
|
||||
var s string = "hi"
|
||||
```
|
||||
|
||||
* var s int = "hi"
|
||||
* s := "hi" *CORRECT*
|
||||
* s, p := 2, 3
|
||||
|
||||
## Which declaration is equal to the following declaration?
|
||||
```go
|
||||
var n = 10
|
||||
```
|
||||
|
||||
* n := 10.0
|
||||
* m, n := 1, 0
|
||||
* var n int = 10 *CORRECT*
|
||||
|
||||
## What's the type of the `s` variable?
|
||||
```go
|
||||
s := "hmm..."
|
||||
```
|
||||
|
||||
* bool
|
||||
* string *CORRECT*
|
||||
* int
|
||||
* float64
|
||||
|
||||
## What's the type of the `b` variable?
|
||||
```go
|
||||
b := true
|
||||
```
|
||||
|
||||
* bool *CORRECT*
|
||||
* string
|
||||
* int
|
||||
* float64
|
||||
|
||||
## What's the type of the `i` variable?
|
||||
```go
|
||||
i := 42
|
||||
```
|
||||
|
||||
* bool
|
||||
* string
|
||||
* int *CORRECT*
|
||||
* float64
|
||||
|
||||
## What's the type of the `f` variable?
|
||||
```go
|
||||
f := 6.28
|
||||
```
|
||||
|
||||
* bool
|
||||
* string
|
||||
* int
|
||||
* float64 *CORRECT*
|
||||
|
||||
## What's the value of the `x` variable?
|
||||
|
||||
```go
|
||||
y, x := false, 20
|
||||
```
|
||||
|
||||
* 10
|
||||
* 20 *CORRECT*
|
||||
* false
|
||||
|
||||
## What's the value of the `x` variable?
|
||||
|
||||
```go
|
||||
y, x := false, 20
|
||||
x, z := 10, "hi"
|
||||
```
|
||||
|
||||
* 10 *CORRECT*
|
||||
* 20
|
||||
* false
|
||||
|
||||
## Which following declaration can be used in the package scope?
|
||||
|
||||
* x := 10
|
||||
* y, x := 10, 5
|
||||
* var x, y = 5, 10
|
||||
@@ -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*
|
||||
25
06-variables/05-type-conversion/01-destructive/main.go
Normal file
25
06-variables/05-type-conversion/01-destructive/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
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
speed := 100 // int
|
||||
force := 2.5 // float64
|
||||
|
||||
// ERROR: invalid op
|
||||
// speed = speed * force
|
||||
|
||||
// conversion can be a destructive operation
|
||||
// `force` loses its fractional part...
|
||||
|
||||
speed = speed * int(force)
|
||||
|
||||
fmt.Println(speed)
|
||||
}
|
||||
31
06-variables/05-type-conversion/02-correct/main.go
Normal file
31
06-variables/05-type-conversion/02-correct/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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"
|
||||
|
||||
// order of conversion matters...
|
||||
|
||||
func main() {
|
||||
speed := 100
|
||||
force := 2.5
|
||||
|
||||
fmt.Printf("speed : %T\n", speed)
|
||||
fmt.Printf("conversion: %T\n", float64(speed))
|
||||
fmt.Printf("expression: %T\n", float64(speed)*force)
|
||||
|
||||
// TYPE MISMATCH:
|
||||
// speed is int
|
||||
// expression is float64
|
||||
// speed = float64(speed) * force
|
||||
|
||||
// CORRECT: int * int
|
||||
speed = int(float64(speed) * force)
|
||||
|
||||
fmt.Println(speed)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user