Initial commit

This commit is contained in:
Inanc Gumus
2018-10-13 23:30:21 +03:00
commit cde4e6632c
567 changed files with 17896 additions and 0 deletions

View 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
import "fmt"
func main() {
// when an integer and a float value used together
// in an expression, the result always becomes
// a float value
fmt.Println(8 * -4.0) // -32.0 not -32
// two integer values result in an integer value
fmt.Println(-4 / 2)
// remainder operator
// it can only used with integers
fmt.Println(5 % 2)
// fmt.Println(5.0 % 2) // wrong
// addition operators
fmt.Println(1 + 2.5)
fmt.Println(2 - 3)
// negation operator
fmt.Println(-(-2))
fmt.Println(- -2) // this also works
}

View 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() {
var (
myAge = 30
yourAge = 35
average float64
)
average = float64(myAge+yourAge) / 2
fmt.Println(average)
}

View 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() {
ratio := 1.0 / 10.0
// after 10 operations
// the inaccuracy is clear
//
// BTW, don't mind about this loop syntax for now
// I'm going to explain it afterwards
for range [...]int{10: 0} {
ratio += 1.0 / 10.0
}
fmt.Printf("%.60f", ratio)
}

View 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() {
// Go compiler sees these numbers as integers,
// since, there are no fractional parts in
// integer values,
// So, the result becomes 1 instead of 1.5
// So, ratio variable here is an int variable,
// it's because, 3 divided by 2 results
// in an integer.
ratio := 3 / 2
fmt.Printf("%d", ratio)
}

View 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"
func main() {
// When you use a float value with an integer
// in a calculation,
// the result always becomes a float.
ratio := 3.0 / 2
// OR:
// ratio = 3 / 2.0
// OR - if 3 is inside an int variable:
// n := 3
// ratio = float64(n) / 2
fmt.Printf("%f", ratio)
}

View File

@@ -0,0 +1,30 @@
// 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("sum :", 3+2) // sum - int
fmt.Println("sum :", 2+3.5) // sum - float64
fmt.Println("dif :", 3-1) // difference - int
fmt.Println("dif :", 3-0.5) // difference - float64
fmt.Println("prod:", 4*5) // product - int
fmt.Println("prod:", 5*2.5) // product - float64
fmt.Println("quot:", 8/2) // quotient - int
fmt.Println("quot:", 8/1.5) // quotient - float64
// remainder is only for integers
fmt.Println("rem :", 8%3)
// fmt.Println("rem:", 8.0%3) // error
// you can do this
// since the fractional part of a float is zero
f := 8.0
fmt.Println("rem :", int(f)%3)
}

View 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
import "fmt"
func main() {
// what's the value of the ratio?
// 3 / 2 = 1.5?
var ratio float64 = 3 / 2
fmt.Println(ratio)
// explain
// above expression equals to this:
ratio = float64(int(3) / int(2))
fmt.Println(ratio)
// how to fix it?
//
// remember, when one of the values is a float value
// the result becomes a float
ratio = float64(3) / 2
fmt.Println(ratio)
// or
ratio = 3.0 / 2
fmt.Println(ratio)
}

View 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"
func main() {
fmt.Println(
2+2*4/2,
2+((2*4)/2), // same as above
)
fmt.Println(
1+4-2,
(1+4)-2, // same as above
)
fmt.Println(
(2+2)*4/2,
(2+2)*(4/2), // same as above
)
}

View 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() {
n, m := 1, 5
fmt.Println(2 + 1*m/n)
fmt.Println(2 + ((1 * m) / n)) // same as above
// let's change the precedence using parentheses
fmt.Println(((2 + 1) * m) / n)
}

View File

@@ -0,0 +1,30 @@
// 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() {
// // Precedence: Order of expressions
// // Multiplication operators runs first: * and /
// fmt.Println(
// 1 + 5 - 3*10/2,
// )
// // 3 * 10 = 30
// // 30 / 2 = 15
// // 1 + 5 = 6
// // 6 - 15 = -9
// // **** TIP ****
// // Use parentheses to change the order of evaluation.
// // First, (1+5-3), then (10/2) will be calculated.
// fmt.Println(
// (1 + 5 - 3) * (10 / 2),
// )
}

View 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() {
celsius := 35.
// Wrong formula : 9*celsius + 160 / 5
// Correct formula: (9*celsius + 160) / 5
fahrenheit := (9*celsius + 160) / 5
fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit)
}

View 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
import "fmt"
func main() {
var n int
// ALTERNATIVES:
// n = n + 1
// n += 1
// BETTER:
n++
fmt.Println(n)
}

View 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
import "fmt"
func main() {
n := 10
// ALTERNATIVES:
// n = n - 1
// n -= 1
// BETTER:
n--
fmt.Println(n)
}

View File

@@ -0,0 +1,30 @@
// 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"
// incdec is a statement
func main() {
var counter int
// following "statements" are correct:
counter++ // 1
counter++ // 2
counter++ // 3
counter-- // 2
fmt.Printf("There are %d line(s) in the file\n",
counter)
// the following "expressions" are incorrect:
// counter = 5+counter--
// counter = ++counter + counter--
}

View 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() {
width, height := 5., 12.
// calculates the area of a rectangle
area := width * height
fmt.Printf("%gx%g=%g\n", width, height, area)
area = area - 10 // decreases area by 10
area = area + 10 // increases area by 10
area = area * 2 // doubles the area
area = area / 2 // divides the area by 2
fmt.Printf("area=%g\n", area)
// // ASSIGNMENT OPERATIONS
area -= 10 // decreases area by 10
area += 10 // increases area by 10
area *= 2 // doubles the area
area /= 2 // divides the area by 2
fmt.Printf("area=%g\n", area)
// finds the remainder of area variable
// since: area is float, this won't work:
// area %= 7
// this works
area = float64(int(area) % 7)
fmt.Printf("area=%g\n", area)
}

View 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"
"os"
"strconv"
)
func main() {
arg := os.Args[1]
// feet is a float64 now
feet, _ := strconv.ParseFloat(arg, 64)
meters := feet * 0.3048
fmt.Printf("%f feet is %f meters.\n", feet, meters)
// pretty print it:
// fmt.Printf("%g feet is %g meters.\n", feet, meters)
}

View 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. Print the sum of 50 and 25
// 2. Print the difference of 50 and 15.5
// 3. Print the product of 50 and 0.5
// 4. Print the quotient of 50 and 0.5
// 5. Print the remainder of 25 and 3
// 6. Print the negation of `5 + 2`
//
// EXPECTED OUTPUT
// 75
// 34.5
// 25
// 100
// 1
// -7
// ---------------------------------------------------------
func main() {
// ADD YOUR CODE BELOW
// USE `fmt.Println` for each question
// DO NOT TOUCH THIS
// fmt.Println(x)
}

View 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"
func main() {
fmt.Println(50 + 25)
fmt.Println(50 - 15.5)
fmt.Println(50 * 0.5)
fmt.Println(50 / 0.5)
fmt.Println(25 % 3)
fmt.Println(-(5 + 2))
}

View 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
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// Fix the program to print 2.5 instead of 2
//
// EXPECTED OUTPUT
// 2.5
// ---------------------------------------------------------
func main() {
x := 5 / 2
fmt.Println(x)
}

View 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() {
// Below solutions are correct:
x := 5. / 2
// x := 5 / 2.
// x := float64(5) / 2
// x := 5 / float64(2)
fmt.Println(x)
}

View 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
// Change the expressions to produce the expected outputs
//
// RESTRICTION
// Use parentheses to change the precedence
// ---------------------------------------------------------
func main() {
// This expression should print 20
fmt.Println(10 + 5 - 5 - 10)
// This expression should print -16
fmt.Println(-10 + 0.5 - 1 + 5.5)
// This expression should print -25
fmt.Println(5 + 10*2 - 5)
// This expression should print 0.5
fmt.Println(0.5*2 - 1)
// This expression should print 24
fmt.Println(3 + 1/2*10 + 4)
// This expression should print 15
fmt.Println(10 / 2 * 10 % 7)
}

View File

@@ -0,0 +1,30 @@
// 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() {
// 10 + 5 - 5 - 10
fmt.Println(10 + 5 - (5 - 10))
// -10 + 0.5 - 1 + 5.5
fmt.Println(-10 + 0.5 - (1 + 5.5))
// 5 + 10*2 - 5
fmt.Println(5 + 10*(2-5))
// 0.5*2 - 1
fmt.Println(0.5 * (2 - 1))
// 3 + 1/2*10 + 4
fmt.Println((3+1)/2*10 + 4)
// 10 / 2 * 10 % 7
fmt.Println(10 / 2 * (10 % 7))
}

View 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. Increase the `counter` 5 times
// 2. Decrease the `factor` 2 times
// 3. Print the product of counter and factor
//
// RESTRICTION
// Use only the incdec statements
//
// EXPECTED OUTPUT
// -75
// ---------------------------------------------------------
func main() {
// DO NOT TOUCH THIS
counter, factor := 45, 0.5
// TYPE YOUR CODE BELOW
// ...
// LASTLY: REMOVE THE CODE BELOW
_, _ = counter, factor
}

View 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() {
counter, factor := 45, 0.5
counter++
counter++
counter++
counter++
counter++
factor--
factor--
fmt.Println(float64(counter) * factor)
}

View 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"
// ---------------------------------------------------------
// EXERCISE
// 1. Write the simplest line of code to increase
// the counter variable by 1.
//
// 2. Write the simplest line of code to decrease
// the counter variable by 1.
//
// 3. Write the simplest line of code to increase
// the counter variable by 5.
//
// 4. Write the simplest line of code to multiply
// the counter variable by 10.
//
// 5. Write the simplest line of code to divide
// the counter variable by 5.
//
// EXPECTED OUTPUT
// 10
// ---------------------------------------------------------
func main() {
// DO NOT CHANGE THE CODE BELOW
var counter int
// TYPE YOUR CODE HERE
// DO NOT CHANGE THE CODE BELOW
fmt.Println(counter)
}

View 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
import "fmt"
func main() {
var counter int
counter++
counter--
counter += 5
counter *= 10
counter /= 5
fmt.Println(counter)
}

View 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
// Simplify the code (refactor)
//
// RESTRICTION
// Use only the incdec and assignment operations
//
// EXPECTED OUTPUT
// 3
// ---------------------------------------------------------
func main() {
width, height := 10, 2
width = width + 1
width = width + height
width = width - 1
width = width - height
width = width * 20
width = width / 25
width = width % 5
fmt.Println(width)
}

View 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() {
width, height := 10, 2
width++
width += height
width--
width -= height
width *= 20
width /= 25
width %= 5
fmt.Println(width)
}

View File

@@ -0,0 +1,45 @@
// 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
// Calculate the area of a circle from the given radius
//
// CIRCLE AREA FORMULA
// area = πr²
// https://en.wikipedia.org/wiki/Area#Circles
//
// HINT
// For PI you can use `math.Pi`
//
// EXPECTED OUTPUT
// 314.1592653589793
//
// BONUS EXERCISE!
// 1. Print the area as 314.16
// 2. To do that you need to use the correct Printf verb :)
// Instead of `%g` verb below.
// ---------------------------------------------------------
func main() {
var (
radius = 10.
area float64
)
// ADD YOUR CODE HERE
// ...
// DO NOT TOUCH THIS
fmt.Printf("radius: %g -> area: %g\n", radius, area)
}

View 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"
"math"
)
func main() {
var (
radius = 10.
area float64
)
area = math.Pi * radius * radius
fmt.Printf("radius: %g -> area: %.2f\n",
radius, area)
// ALTERNATIVE:
// math.Pow calculates the power of a float number
// area = math.Pi * math.Pow(radius, 2)
}

View 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"
)
// ---------------------------------------------------------
// EXERCISE
// 1. Get the radius from the command-line
// 2. Convert it to a float64
// 3. Calculate the surface area of a sphere
//
// SPHERE SURFACE AREA FORMULA
// area = 4πr²
// https://en.wikipedia.org/wiki/Sphere#Surface_area
//
// RESTRICTION
// Use `math.Pow` to calculate the area
// Read its documentation to see how it works.
// https://golang.org/pkg/math/#Pow
//
// EXPECTED OUTPUT
// 1256.64
// ---------------------------------------------------------
func main() {
var radius, area float64
// ADD YOUR CODE HERE
// ...
// DO NOT TOUCH THIS
fmt.Printf("radius: %g -> area: %.2f\n", radius, area)
}

View 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"
"math"
"os"
"strconv"
)
func main() {
var radius, area float64
radius, _ = strconv.ParseFloat(os.Args[1], 64)
area = 4 * math.Pi * math.Pow(radius, 2)
fmt.Printf("radius: %g -> area: %.2f\n",
radius, area)
}

View 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. Get the radius from the command-line
// 2. Convert it to a float64
// 3. Calculate the volume of a sphere
//
// SPHERE VOLUME FORMULA
// https://en.wikipedia.org/wiki/Sphere#Enclosed_volume
//
// RESTRICTION
// Use `math.Pow` to calculate the volume
//
// EXPECTED OUTPUT
// 4188.79
// ---------------------------------------------------------
func main() {
var radius, vol float64
// ADD YOUR CODE HERE
// ...
// DO NOT TOUCH THIS
fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol)
}

View 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"
"math"
"os"
"strconv"
)
func main() {
var radius, vol float64
radius, _ = strconv.ParseFloat(os.Args[1], 64)
vol = (4 * math.Pi * math.Pow(radius, 3)) / 3
fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol)
}

View File

@@ -0,0 +1,95 @@
## Which group of operators below is arithmetic operators?
1. **, /, ^, !, ++, --
2. *, /, %, +, - *CORRECT*
3. &, |, +, -, /
## Which value below you can use with a remainder operator?
1. 3.54
2. true
3. 57 *CORRECT*
4. "Try Me!"
> 4. Nice Try. But, that's not right. Sorry.
> 3. That's right. The remainder operator only works on integer values.
## What's the result of this expression?
```go
8 % 3
```
1. 4
2. 2
3. 0
4. 1 *CORRECT*
## What's the result of this expression?
```go
-(3 * -2)
```
1. -6
2. -1
3. 0
4. 6 *CORRECT*
## What's the result of this expression?
```go
var degree float64 = 10 / 4
```
1. 2.5
2. 2.49
3. 2 *CORRECT*
4. 0
> 3. That's right. An integer value cannot contain fractional parts.
## What's the result of this expression?
```go
var degree float64 = 3. / 2
```
1. 1.5 *CORRECT*
2. 1.49
3. 1
4. 0
> 1. That's right. `3.` makes the whole expression a float value.
## What's the type of the `x` variable?
```go
x := 5 * 2.
```
1. int
2. float64 *CORRECT*
3. bool
4. string
> 1. Look closely to 2 there.
> 2. Why? Because, `2.` there makes the expressions a float value. Cool.
> 3. Oh, come on! Life is not always true and false.
> 4. I can't see any double-quotes or back-quotes, can you?
## What's the type of the `x` variable?
```go
x := 5 * -(2)
```
1. int *CORRECT*
2. float64
3. bool
4. string
> 1. Why? Because, there only integer numbers.
> 2. I can't see any fractional parts there, can you?
> 3. Oh, come on! Life is not always true and false.
> 4. I can't see any double-quotes or back-quotes, can you?
## Which kind of values can result in inaccurate calculations?
1. integers
2. floats *CORRECT*
3. bools
4. strings

View File

@@ -0,0 +1,44 @@
## What's the result of the expression?
```go
5 - 2 * 5 + 7
```
1. 2 *CORRECT*
2. 22
3. -19
4. 36
5. -12
## What's the result of the expression?
```go
5 - (2 * 5) + 7
```
1. 2
2. 22 *CORRECT*
3. -19
4. 36
5. -12
## What's the result of the expression?
```go
5 - 2 * (5 + 7)
```
1. 2
2. 22
3. -19 *CORRECT*
4. 36
5. -12
## What's the result of the expression?
```go
5. -(2 * 5 + 7)
```
1. 2
2. 22
3. -19
4. -12
5. -12.0 *CORRECT*
> 4. You're close but remember! The result of an expression with floats and integers is always a float.

View File

@@ -0,0 +1,98 @@
## Which expression increases `n` by 1?
```go
var n float64
```
1. `n = +1`
2. `n = n++`
3. `n = n + 1` *CORRECT*
4. `++n`
> 1. This just assigns 1 to n.
> 2. IncDec statement can't be used as an operator.
> 4. Go doesn't support prefix incdec notation.
## Which expression decreases `n` by 1?
```go
var n int
```
1. `n = -1`
2. `n = n--`
3. `n = n - 1` *CORRECT*
4. `--n`
> 1. This just assigns -1 to n.
> 2. IncDec statement can't be used as an operator.
> 4. Go doesn't support prefix incdec notation.
## Which code below equals to `n = n + 1`?
1. `n++` *CORRECT*
2. `n = n++`
3. `++n`
4. `n = n ++ 1`
> 2. IncDec statement can't be used as an operator.
> 3. Go doesn't support prefix incdec notation.
> 4. What's that? ++?
## Which code below equals to `n = n + 1`?
1. `n = n++`
2. `n += 1` *CORRECT*
3. `++n`
4. `n = n ++ 1`
> 1. IncDec statement can't be used as an operator.
> 3. Go doesn't support prefix incdec notation.
> 4. What's that? ++?
## Which code below equals to `n -= 1`?
1. `n = n--`
2. `n += 1--`
3. `n--` *CORRECT*
4. `--n`
> 1. IncDec statement can't be used as an operator.
> 2. IncDec statement can't be used as an operator. And also, you can't use it with `1--`. The value should be addressable. You're going to learn what that means soon.
> 4. Go doesn't support prefix incdec notation.
## Which code below divides the `length` by 10?
1. `length = length // 10`
2. `length /= 10` *CORRECT*
3. `length //= 10`
> 1. What's that? `//`?
> 2. That's right. This equals to: `length = length / 10`
> 3. What's that? `//=`?
## Which code below equals to `x = x % 2`?
1. `x = x / 2`
2. `x =% x`
3. `x %= x` *CORRECT*
> 1. This is a division. You need to use the remainder operator.
> 2. Close... But, the `%` operator is on the wrong side of the assignment.
## Which function below converts a string value into a float value?
1. `fmtconv.ToFloat`
2. `conv.ParseFloat`
3. `strconv.ParseFloat` *CORRECT*
4. `strconv.ToFloat`
## Which code is correct?
If you don't remember it, this its function signature:
```go
func ParseFloat(s string, bitSize int) (float64, error)
```
1. `strconv.ParseFloat("10", 128)`
2. `strconv.ParseFloat("10", 64)` *CORRECT*
3. `strconv.ParseFloat("10", "64")`
4. `strconv.ParseFloat(10, 64)`
> 1. There are no 128-bit floating point values in Go (Actually there are, but they only belong to the compile-time).

View 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() {
// The type of a string and a raw string literal
// is the same. They both are strings.
//
// So, they both can be used as a string value.
var s string
s = "how are you?"
s = `how are you?`
fmt.Println(s)
// string literal
s = "<html>\n\t<body>\"Hello\"</body>\n</html>"
fmt.Println(s)
// raw string literal
s = `
<html>
<body>"Hello"</body>
</html>`
fmt.Println(s)
// windows path
fmt.Println("c:\\my\\dir\\file") // string literal
fmt.Println(`c:\my\dir\file`) // raw string literal
}

View File

@@ -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() {
name, last := "carl", "sagan"
fmt.Println(name + " " + last)
}

View 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() {
name, last := "carl", "sagan"
// assignment operation using string concat
name += " edward"
// equals to this:
// name = name + " edward"
fmt.Println(name + " " + last)
}

View File

@@ -0,0 +1,51 @@
// 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"
"strconv"
)
func main() {
fmt.Println(
"hello" + ", " + "how" + " " + "are" + " " + "today?",
)
// you can combine raw string and string literals
fmt.Println(
`hello` + `, ` + `how` + ` ` + `are` + ` ` + "today?",
)
// ------------------------------------------
// Converting non-string values into string
// ------------------------------------------
eq := "1 + 2 = "
sum := 1 + 2
// invalid op
// string concat op can only be used with strings
// fmt.Println(eq + sum)
// you need to convert it using strconv.Itoa
// Itoa = Integer to ASCII
fmt.Println(eq + strconv.Itoa(sum))
//
// invalid op
// eq = true + " " + false
eq = strconv.FormatBool(true) +
" " +
strconv.FormatBool(false)
fmt.Println(eq)
}

View 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() {
name := "carl"
// strings are made up of bytes
// len function counts the bytes in a string value
fmt.Println(len(name))
}

View 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"
"unicode/utf8"
)
func main() {
// strings are made up of bytes
// len function counts the bytes in a string value.
//
// This string literal contains unicode characters.
//
// And, unicode characters can be 1-4 bytes.
// So, "İnanç" is 7 bytes long, not 5.
//
// İ = 2 bytes
// n = 1 byte
// a = 1 byte
// n = 1 byte
// ç = 2 bytes
// TOTAL = 7 bytes
name := "İnanç"
fmt.Printf("%q is %d bytes\n", name, len(name))
// To get the actual characters (or runes) inside
// a utf-8 encoded string value, you should do this:
fmt.Printf("%q is %d characters\n",
name, utf8.RuneCountInString(name))
}

View 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
import (
"os"
"strings"
)
// NOTE: You should always pass it at least one argument
func main() {
msg := os.Args[1]
// it's important to calculate things only once
// so, do not call the repeat function twice
// calling it once is enough
marks := strings.Repeat("!", len(msg))
s := marks + msg + marks
s = strings.ToUpper(s)
// you can also type this program more concisely
// like this:
//
// msg := strings.ToUpper(os.Args[1])
// marks := strings.Repeat("!", len(msg))
// fmt.Println(marks + msg + marks)
}

View 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"
"os"
"strings"
)
// NOTE: You should always pass it at least one argument
func main() {
msg := os.Args[1]
l := len(msg)
s := msg + strings.Repeat("!", l)
s = strings.ToUpper(s)
fmt.Println(s)
}

View 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
import "fmt"
// ---------------------------------------------------------
// EXERCISE
// 1. Change the following program
// 2. It should use a raw string literal instead
//
// HINT
// Run this program first to see its output.
// Then you can easily understand what it does.
//
// EXPECTED OUTPUT
// Your solution should output the same as this program.
// Only that it should use a raw string literal instead.
// ---------------------------------------------------------
func main() {
// HINTS:
// \\ equals to backslash character
// \n equals to newline character
path := "c:\\program files\\duper super\\fun.txt\n" +
"c:\\program files\\really\\funny.png"
fmt.Println(path)
}

View 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() {
// this one uses a raw string literal
// can you see how readable it is?
// compared to the previous one?
path := `c:\program files\duper super\fun.txt
c:\program files\really\funny.png`
fmt.Println(path)
}

View File

@@ -0,0 +1,42 @@
// 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. Change the following program
// 2. It should use a raw string literal instead
//
// HINT
// Run this program first to see its output.
// Then you can easily understand what it does.
//
// EXPECTED OUTPUT
// Your solution should output the same as this program.
// Only that it should use a raw string literal instead.
// ---------------------------------------------------------
func main() {
// HINTS:
// \t equals to TAB character
// \n equals to newline character
// \" equals to double-quotes character
json := "\n" +
"{\n" +
"\t\"Items\": [{\n" +
"\t\t\"Item\": {\n" +
"\t\t\t\"name\": \"Teddy Bear\"\n" +
"\t\t}\n" +
"\t}]\n" +
"}\n"
fmt.Println(json)
}

View 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
import "fmt"
func main() {
// this one uses a raw string literal
// can you see how readable it is?
// compared to the previous one?
json := `
{
"Items": [{
"Item": {
"name": "Teddy Bear"
}
}]
}`
fmt.Println(json)
}

View File

@@ -0,0 +1,44 @@
// 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. Initialize the name variable
// by getting input from the command line
//
// 2. Use concatenation operator to concatenate it
// with the raw string literal below
//
// NOTE
// You should concatenate the name variable in the correct
// place.
//
// EXPECTED OUTPUT
// Let's say that you run the program like this:
// go run main.go inanç
//
// Then it should output this:
// hi inanç!
// how are you?
// ---------------------------------------------------------
func main() {
// uncomment the code below
// name := "and get the name from the command-line"
// replace and concatenate the `name` variable
// after `hi ` below
msg := `hi CONCATENATE-NAME-VARIABLE-HERE!
how are you?`
fmt.Println(msg)
}

View 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"
"os"
)
func main() {
name := os.Args[1]
msg := `hi ` + name + `!
how are you?`
fmt.Println(msg)
}

View 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
import (
"fmt"
"os"
)
// ---------------------------------------------------------
// EXERCISE
// 1. Change the following program to work with unicode
// characters.
//
// INPUT
// "İNANÇ"
//
// EXPECTED OUTPUT
// 5
// ---------------------------------------------------------
func main() {
// Currently it returns 7
// Because, it counts the bytes...
// It should count the runes (codepoints) instead.
//
// When you run it with "İNANÇ", it should return 5 not 7.
length := len(os.Args[1])
fmt.Println(length)
}

View 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"
"os"
"unicode/utf8"
)
func main() {
length := utf8.RuneCountInString(os.Args[1])
fmt.Println(length)
}

View 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
import (
"fmt"
"os"
"strings"
)
// ---------------------------------------------------------
// EXERCISE
// Change the Banger program the work with Unicode
// characters.
//
// INPUT
// "İNANÇ"
//
// EXPECTED OUTPUT
// İNANÇ!!!!!
// ---------------------------------------------------------
func main() {
msg := os.Args[1]
s := msg + strings.Repeat("!", len(msg))
fmt.Println(s)
}

View 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"
"os"
"strings"
"unicode/utf8"
)
func main() {
msg := os.Args[1]
l := utf8.RuneCountInString(msg)
s := msg + strings.Repeat("!", l)
fmt.Println(s)
}

View 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. Look at the documentation of strings package
// 2. Find a function that changes the letters into lowercase
// 3. Get a value from the command-line
// 4. Print the given value in lowercase letters
//
// INPUT
// "SHEPARD"
//
// EXPECTED OUTPUT
// shepard
// ---------------------------------------------------------
func main() {
}

View 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"
"os"
"strings"
)
func main() {
fmt.Println(strings.ToLower(os.Args[1]))
}

View 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"
)
// ---------------------------------------------------------
// EXERCISE
// 1. Look at the documentation of strings package
// 2. Find a function that trims the spaces from
// the given string
// 3. Trim the text variable and print it
//
// EXPECTED OUTPUT
// The weather looks good.
// I should go and play.
// ---------------------------------------------------------
func main() {
msg := `
The weather looks good.
I should go and play.
`
fmt.Println(msg)
}

View 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"
"strings"
)
func main() {
msg := `
The weather looks good.
I should go and play.
`
fmt.Println(strings.TrimSpace(msg))
}

View 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. Look at the documentation of strings package
// 2. Find a function that trims the spaces from
// only the right-most part of the given string
// 3. Trim it from the right part only
// 4. Print the number of characters it contains.
//
// RESTRICTION
// Your program should work with unicode string values.
//
// EXPECTED OUTPUT
// 5
// ---------------------------------------------------------
func main() {
// currently it prints 17
// it should print 5
name := "inanç "
fmt.Println(len(name))
}

View 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
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
name := "inanç "
name = strings.TrimRight(name, " ")
l := utf8.RuneCountInString(name)
fmt.Println(l)
}

View File

@@ -0,0 +1,194 @@
## What's the result of this expression?
```go
"\"Hello\\"" + ` \"World\"`
```
1. "Hello" "World"
2. "Hello" \"World\" *CORRECT*
3. "Hello" `"World"`
4. "\"Hello\" `\"World\"`"
> 1. Go doesn't interpret the escape sequences in raw string literals.
> 2. That's right. Go interprets `\"` as `"` but it doesn't do so for ` \"World\"`.
## What's the best way to represent the following text in the code?
```xml
<xml>
<items>
<item>"Teddy Bear"</item>
</items>
</xml>
```
1. *CORRECT*
```go
`<xml>
<items>
<item>"Teddy Bear"</item>
</items>
</xml>`
```
2.
```go
"<xml>
<items>
<item>"Teddy Bear"</item>
</items>
</xml>"
```
3.
```go
"<xml>
<items>
<item>\"Teddy Bear\"</item>
</items>
</xml>"
```
4.
```go
`<xml>
<items>
<item>\"Teddy Bear\"</item>
</items>
</xml>`
```
> 2-3. You can't write a string literal like that. It can't be multiple-lines.
> 4. You don't need to use escape sequences inside raw string literals.
## What's the result of the following expression?
```go
len("lovely")
```
1. 7
2. 8
3. 6 *CORRECT*
4. 0
> 2. Remember! "a" is 1 char. `a` is also 1 char.
## What's the result of the following expression?
```go
len("very") + len(`\"cool\"`)
```
1. 8
2. 12 *CORRECT*
3. 16
4. 10
> 1. There are also double-quotes, count them as well.
> 2. That's right. Go doesn't interpreted \" in raw string literals.
> 3. Remember! "very" is 4 characters. `very` is also 4 characters.
> 4. Remember! Go doesn't interpreted \" in raw string literals.
## What's the result of the following expression?
```go
len("very") + len("\"cool\"")
```
1. 8
2. 12
3. 16
4. 10 *CORRECT*
> 1. There are also double-quotes, count them as well.
> 2. Remember! Go interprets escape sequences in string literals.
> 4. That's right. Go does interpret \" in a string literal. So, "\"" means ", which is 1 character.
## What's the result of the following expression?
```go
len("péripatéticien")
```
**HINT:** é is 2 bytes long. And, the len function counts the bytes not the letters.
**USELESS INFORMATION:** "péripatéticien" means "wanderer".
1. 14
2. 16 *CORRECT*
3. 18
4. 20
> 1. Remember! é is 2 bytes long.
> 2. An english letter is 1 byte long. However, é is 2 bytes long. So, that makes up 16 bytes. Cool.
> 3. You didn't count the double-quotes, did you?
## How can you find the correct length of the characters in this string literal?
```go
"péripatéticien"
```
1. `len(péripatéticien)`
2. `len("péripatéticien")`
3. `utf8.RuneCountInString("péripatéticien")` *CORRECT*
4. `unicode/utf8.RuneCountInString("péripatéticien")`
> 1. Where are the double-quotes?
> 2. This only finds the bytes in a string value.
> 4. You're close. But, the package's name is utf8 not unicode/utf8.
## What's the result of the following expression?
```go
utf8.RuneCountInString("péripatéticien")
```
1. 16
2. 14 *CORRECT*
3. 18
4. 20
> 1. This is its byte count. `RuneCountInString` counts the runes (codepoints) not the bytes.
> 2. That's right. `RuneCountInString` returns the number of runes (codepoints) in a string value.
## Which package contains string manipulation functions?
1. string
2. unicode/utf8
3. strings *CORRECT*
4. unicode/strings
## What's the result of this expression?
```go
strings.Repeat("*x", 3) + "*"
```
**HINT:** Repeat function repeats the given string.
1. `*x*x*x`
2. `x*x*x`
3. `*x3`
4. `*x*x*x*` *CORRECT*
> 1. You're close but you missed the concatenation at the end.
> 2. Look closely.
> 3. Wow! You should really watch the lectures again. Sorry.
> 4. That's right. Repeat function repeats the given string. And, the concatenation operator combines the strings.
## What's the result of this expression?
```go
strings.ToUpper("bye bye ") + "see you!"
```
1. `bye bye see you!`
2. `BYE BYE SEE YOU!`
3. `bye bye + see you!`
4. `BYE BYE see you!` *CORRECT*
> 1. You missed the ToUpper?
> 2. You're close but look closely. ToUpper only changes the first part of the string there.
> 3. Not even close. Sorry.
> 4. Perfect! Good catch! ToUpper only changes the first part of the string there.