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,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.