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,14 @@
// 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 bye() {
fmt.Println("Bye!")
}

View File

@@ -0,0 +1,14 @@
// 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 hey() {
fmt.Println("Hey!")
}

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() {
fmt.Println("Hello!")
// You can access functions from other files
// which are in the same package
// Here, `main()` can access `bye()` and `hey()`
// It's because: bye.go, hey.go and main.go
// are in the main package.
bye()
hey()
}

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
// file scope
import "fmt"
// package scope
const ok = true
// package scope
func main() { // block scope starts
var hello = "Hello"
// hello and ok are visible here
fmt.Println(hello, ok)
} // block scope ends

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
func nope() { // block scope starts
// hello and ok are only visible here
const ok = true
var hello = "Hello"
_ = hello
} // block scope ends
func main() { // block scope starts
// hello and ok are not visible here
// ERROR:
// fmt.Println(hello, ok)
} // block scope ends

View File

@@ -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"
// I didn't talk about this in a lecture
// As a side-note, I wanted to put it here
// Please review it.
var declareMeAgain = 10
func nested() { // block scope starts
// declares the same variable
// they both can exist together
// this one only belongs to this scope
// package's variable is still intact
var declareMeAgain = 5
fmt.Println("inside nope:", declareMeAgain)
} // block scope ends
func main() { // block scope starts
fmt.Println("inside main:", declareMeAgain)
nested()
// package-level declareMeAgain isn't effected
// from the change inside the nested func
fmt.Println("inside main:", declareMeAgain)
} // block scope ends

View File

@@ -0,0 +1,14 @@
// 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 bye() {
fmt.Println("Bye!")
}

View File

@@ -0,0 +1,14 @@
// 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 hey() {
fmt.Println("Hey!")
}

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"
func main() {
fmt.Println("Hello!")
// two files belong to the same package
// calling `bye()` of bye.go here
bye()
}
// EXERCISE: Remove the comments from the below function
// And analyze the error message
// func bye() {
// fmt.Println("Bye!")
// }
// ***** EXPLANATION *****
//
// ERROR: "bye" function "redeclared"
// in "this block"
//
// "this block" means = "main package"
//
// "redeclared" means = you're using the same name
// in the same scope again
//
// main package's scope is:
// all source-code files which are in the same main package

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
// Uncomment below code to see the error
// (Just remove the // characters for all 3 lines below)
// This file cannot see main.go's imported names ("fmt").
// Because, the imported names belong to file scope.
// func bye() {
// fmt.Println("Bye!")
// }

View File

@@ -0,0 +1,14 @@
// 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("Hello!")
}

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"
import f "fmt"
func main() {
fmt.Println("Hello!")
f.Println("There!")
}

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
// ---------------------------------------------------------
// EXERCISE
// Create a few Go files and call their functions from
// the main function.
//
// 1- Create main.go, greet.go and bye.go files
// 2- In main.go: Call greet and bye functions.
// 3- Run `main.go`
//
// HINT
// greet function should be in greet.go
// bye function should be in bye.go
//
// EXPECTED OUTPUT
// hi there
// goodbye
// ---------------------------------------------------------
func main() {
// call functions of the other files here
}

View File

@@ -0,0 +1,14 @@
// 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 bye() {
fmt.Println("goodbye")
}

View File

@@ -0,0 +1,14 @@
// 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 greet() {
fmt.Println("hi there")
}

View 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() {
greet()
bye()
}

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
// ---------------------------------------------------------
// EXERCISE
// 1. Create two files: main.go and printer.go
//
// 2. In printer.go:
// 1. Create a function named hello
// 2. Inside the hello function, print your name
//
// 3. In main.go:
// 1. Create the usual func main
// 2. Call your function just by using its name: hello
// 3. Create a function named bye
// 4. Inside the bye function, print "bye bye"
//
// 4. In printer.go:
// 1. Call the bye function from
// inside the hello function
//
// 5. In main.go:
// 1.
// ---------------------------------------------------------
func main() {
}

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"
func main() {
// as you can see, I don't need to import a package
// and I can call `hello` function here.
//
// this is because, package-scoped names
// are shared in the same package
hello()
// but here, i can't access the fmt package without
// importing it.
//
// this is because, it's in the printer.go's file scope.
// it imports it.
// this main func can also call bye function here
// bye()
}
// printer.go can call this function
//
// this is because, bye function is in the package-scope
// of the main package now.
//
// main func can also call this.
func bye() {
fmt.Println("bye bye")
}

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 hello() {
// only this file can access the imported fmt package
// when others also do so, they can also access
// their own `fmt` "name"
fmt.Println("hi! this is inanc!")
bye()
}

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
// ---------------------------------------------------------
// EXERCISE
// 1- Import fmt package three times with different names
//
// 2- Print a few messages using those imports
//
// EXPECTED OUTPUT
// hello
// hey
// hi
// ---------------------------------------------------------
// ?
// ?
// ?
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"
import f "fmt"
import fm "fmt"
func main() {
fmt.Println("hello")
f.Println("hey")
fm.Println("hi")
}

View File

@@ -0,0 +1,44 @@
## Where to store the source code files that belong to a package?
1. Each file should go into a different directory
2. In a single directory *CORRECT*
## What's a `package clause` in a Go source-code file?
1. It's used for importing a package
2. It's used for letting Go know that the file belong to a package *CORRECT*
3. It's used for declaring a new function
> 1. `import` statement does that.
> 3. `func` statement does that.
## Where you should put the `package clause` in a Go source-code file?
1. As the first code in a Go source code file *CORRECT*
2. As the last code in a Go source code file
3. You can put it anywhere
## How many times you can use `package clause` for a single source code file?
1. Once *CORRECT*
2. None
3. Multiple times
## Which one is a correct usage of `package clause`?
1. `my package`
2. `package main`
3. `pkg main`
## Which one is correct?
1. All files belong to the same package cannot call each others' functions
2. All files belong to the same package can call each others' functions *CORRECT*
## How to run multiple Go files?
1. go run *.*go
2. go build *go
3. go run go
4. go run *.go *CORRECT*
> 4. You can also call it like (assuming there are file1.go file2.go and file3.go in the same directory): go run file1.go file2.go file3.go

View File

@@ -0,0 +1,40 @@
## Which one below is a correct package type in Go?
* Empty package
* Executable package *CORRECT*
* Transferrable package
* Librarian package
## Which package type `go run` can execute?
* Empty package
* Executable package *CORRECT*
* Transferrable package
* Library package
## Which package type that `go build` can compile?
* Empty package
* Temporary package
* Both of executable and library packages *CORRECT*
* Transferrable package
## Which one is an executable package?
* `package main` with `func main` *CORRECT*
* `package Main` with `func Main`
* `package exec` with `func exec`
## Which one is a library package?
* `main package`
* `package lib` *CORRECT*
* `func package`
* `package main` with `func main`
## Which package is used for an executable Go program?
* Empty package
* Executable package *CORRECT*
* Transferrable package
* Library package
## Which package is used for reusability and can be imported?
* Empty package
* Executable package
* Transferrable package
* Library package *CORRECT*

View File

@@ -0,0 +1,117 @@
## What's a scope?
* Executable block of code
* The visibility of the declared names **CORRECT**
* Determines what to run
```go
package awesome
import "fmt"
var enabled bool
func block() {
var counter int
fmt.Println(counter)
}
```
## Which name below is package scoped?
1. awesome
2. fmt
3. enabled **CORRECT**
4. counter
> 3. That's right. `enabled` is out of any functions, so it's a package scoped name. `block()` function is also package scoped; it's out of any function too.
## Which name below is file scoped?
1. awesome
2. fmt **CORRECT**
3. enabled
4. block()
5. counter
> 2. That's right. Imported package names are file scoped. And they can only be used within the same file.
## Which name below is in the scope of the block() func?
1. awesome
2. fmt
3. enabled
4. block()
5. counter **CORRECT**
> 5. That's right. `counter` is declared within the `block()` func, so it's in the scope of the block func. Out of the `block()` func, other code can't see it.
## Can `block()` see `enabled` name?
1. Yes: It's in the package scope **CORRECT**
2. No: It's in the file scope
3. No: It's in the block scope of block()
> 1. All code inside the same package can see all the other package level declared names.
## Can other files in `awesome` package see `counter` name?
1. Yes
2. No: It's in the package scope
3. No: It's in the file scope
4. No: It's in the block scope of block() **CORRECT**
> 4. That's right. None of the other code can see the names inside the `block()` function. Only the code inside the `block()` function can see them (Only to some extend. For example: Inside the block, the code can only see the variables declared before it.)
## Can other files in `awesome` package see `fmt` name?
1. Yes
2. No: It's in the package scope
3. No: It's in the file scope **CORRECT**
4. No: It's in the block scope of block()
> 3. Only the same file can see the imported packages, not the other files whether they're in the same package or not.
## What happens if you declare the same name in the same scope like this:
```go
package awesome
import "fmt"
// declared twice in the package scope
var enabled bool
var enabled bool
func block() {
var counter int
fmt.Println(counter)
}
```
1. The newly declared name will override the previous one.
2. I can't do that. It's already been declared at the package scope. *CORRECT*
3. I can't do that. It's already been declared at the file scope.
> 2. That's right. You can't declare the same name in the same scope. If you could do so, then how would you access it again? Or to which one?
## What happens if you declare the same name in another scope like this:
```go
package awesome
import "fmt"
// declared at the package scope
var enabled bool
func block() {
// also declared in the block scope
var enabled bool
var counter int
fmt.Println(counter)
}
```
1. The newly declared name will override the previous one. *CORRECT*
2. I can't do that. It's already been declared at the package scope.
3. I can't do that. It's already been declared at the file scope.
> 1. Actually, you can declare the same name in the inner scopes like this. `block()`'s scope is inside its package. This means that it can access to its package's scope (but not vice versa). So, `block()`'s scope is under its package's scope. This means that you can declare the same name again. It will override the parent scope's name. They both can be exist together. Check out the example in the course repository to find out.