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

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
*.DS_Store
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.vscode/*

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
// ---------------------------------------------------------
// EXERCISE
// Print your name and your best friend's name using
// Println twice
//
// EXPECTED OUTPUT
// YourName
// YourBestFriendName
//
// BONUS
// Use `go run` first.
// And after that use `go build` and run your program.
// ---------------------------------------------------------
func main() {
// ?
// ?
}

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"
// go run main.go
// go build
// ./solution
func main() {
fmt.Println("Nikola")
fmt.Println("Thomas")
}

View File

@ -0,0 +1,7 @@
// ---------------------------------------------------------
// EXERCISE
// Print your GOPATH using `go env` tool
//
// EXPECTED OUTPUT
// The physical folder path that is referenced by $GOPATH
// ---------------------------------------------------------

View File

@ -0,0 +1,3 @@
You should type this:
go env GOPATH

View File

@ -0,0 +1,39 @@
1. **Run your own program? Say hello to yourself.**
1. Build your program using `go build`
2. And, send it to your friend
(s/he should use be using the same operating system)
(if you're using windows, then hers/his should be
windows too)
3. And then send your program to a friend with a different
operating system.
(So, you should compile your program for her operating system).
**For OSX, type:**
GOOS=darwin GOARCH=386 go build
**For Windows:**
GOOS=windows GOARCH=386 go build
**For Linux:**
GOOS=linux GOARCH=arm GOARM=7 go build
**You can find the full list in here:**
https://golang.org/doc/install/source#environment
2. **Call Print instead of Println** to see what happens.
3. **Call Println or Print with multiple values** by separating them using commas.
4. **Remove double quotes from string literals** and see what happens.
5. **Move the package and import statement** to the bottom of the file and see what happens.
6. **Read Go online documentation**. Take a quick look at the packages and read what they do. Look at their source-code by clicking on their titles.
You don't have to understand anything, just do it. This will warm you up for the upcoming lectures. https://golang.org/pkg
7. Also, **take a tour**: https://tour.golang.org/ See the language features. We're going to talk all about them soon.

View 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 is a special package
// it allows Go to create an executable file
package main
/*
This is a multi-line comment.
import keyword makes another package available
for this .go "file".
import "fmt" lets you access fmt package's functionality
here in this file.
*/
import "fmt"
// "func main" is special.
//
// Go has to know where to start
//
// func main creates a starting point for Go
//
// After compiling the code,
// Go runtime will first run this function
func main() {
// after: import "fmt"
// Println function of "fmt" package becomes available
// Look at what it looks like by typing in the console:
// godoc -src fmt Println
// Println is just an exported function from
// "fmt" package
// Exported = First Letter is uppercase
fmt.Println("Hello Gopher!")
// Go cannot call Println function by itself.
// That's why you need to call it here.
// It only calls `func main` automatically.
// -----
// Go supports Unicode characters in string literals
// And also in source-code: KÖSTEBEK!
//
// Because: Literal ~= Source Code
// EXERCISE: Remove the comments from below --> //
// fmt.Println("Merhaba Köstebek!")
// Unnecessary note:
// "Merhaba Köstebek" means "Hello Gopher"
// in Turkish language
}

View File

@ -0,0 +1,19 @@
## Where you should put your Go source code into?
* Anywhere on my computer
* Under $GOPATH
* Under $GOPATH/src *CORRECT*
## What $GOPATH means?
* It's a file for Go runtime
* Stores Go source code files and compiled packages
* It's a path for gophers to follow
## Do you need to set your $GOPATH?
* Yes
* No: It's stored on my desktop
* No: It's stored under my user path *CORRECT*
## How can you print your $GOPATH?
* Using `ls` command
* Using `go env GOPATH` command *CORRECT*
* Using `go environment` command

View File

@ -0,0 +1,151 @@
## What does the package keyword do in the following program?
```go
package main
func main() {
}
```
1. func
2. package *CORRECT*
3. fmt.Println
4. import
> 1. This keyword is used to declare a new function.
> 2. That's right! package keyword allows you to define which package a Go file belongs to.
> 3. This is not a keyword. It's the Println function of the fmt package.
> 4. This keyword is used to import a package.
## Which keyword is used to declare a new function?
* func *CORRECT*
* package
* Println
* import
## What is a function?
1. It's like a mini-program. It's a reusable and executable block of code. *CORRECT*
2. It allows Go to execute a program.
3. It allows Go to import a package called function.
4. It prints a message to the console.
> 2. Go looks for package main and func main to do that. A function doesn't do that on its own.
> 3. `import` keyword does that.
> 4. For example: `fmt.Println` does that.
## Do you have to call the main function yourself?
1. Yes, so that, I can execute my program.
2. No, Go calls the main function automatically. *CORRECT*
> 1. No, you don't need to call the main function. Go automatically executes it.
## Do you have to call the other functions yourself?
1. Yes, so that, I can execute that function. *CORRECT*
2. Yes, so that, Go can execute my program.
3. No, Go calls the functions automatically.
> 1. That's right. You need to call a function yourself. Go won't execute it automatically. Go only calls the main function automatically (and some other functions which you didn't learn about yet).
> 2. That's only the job of the `func main`. There's only one `func main`.
> 3. Go doesn't call any function automatically except the main func (and some other functions which you didn't learn about yet). So, except the main func, you need to call the functions yourself.
## What does `package main` do?
```go
package main
func main() {
}
```
* It controls everything
* It allows you to properly exit from a program
* It allows you to create an executable Go program *CORRECT*
## What does `func main` do?
```go
package main
func main() {
}
```
1. It contains a package called main
2. Go starts executing your program by using the code inside func main *CORRECT*
3. It prints a message to the console
> 1. main function doesn't contain a package.
> 2. That's right. Go automatically calls the main function to execute your program.
> 3. It doesn't print anything at least directly.
## What does `import "fmt"` do?
```go
package main
import "fmt"
func main() {
fmt.Println("Hi!")
}
```
1. It prints "fmt" to the console
2. It defines a new package called "fmt"
3. It imports the `fmt` package; so you can use its functionalities *CORRECT*
> 1. `fmt.Println` prints a message not the `import "fmt"`.
> 2. `package` keyword does that, not the `import` keyword.
> 3. Yes. For example, after you import the fmt package you can call its Println function to print a message to the console.
## What this program does?
```go
package main
func main() {
}
```
1. It prints a message to the console
2. It's a correct program but it doesn't print anything *CORRECT*
3. It's an incorrect program
> 1. It doesn't print a message. To do that you can use fmt.Println function.
> 2. Yes, it's a correct program but since it doesn't contain fmt.Println it doesn't print anything.
> 3. It's a correct program. It uses the package keyword and it has a main function. So, this is a valid and an executable Go program.
## What does this program print?
```go
package main
func main() {
fmt.Println(Hi! I want to be a Gopher!)
}
```
* Hi! I want to be a Gopher!
* It doesn't print anything
* This program is incorrect *CORRECT*
> 1. It doesn't pass the message to Println wrapped between double-quotes. It should be like: fmt.Println("Hi! I want to be a Gopher")
> 3. It doesn't import "fmt" package. Also see #1.
## What does this program print?
```go
package main
import "fmt"
func main() {
fmt.Println("Hi there!")
}
```
* Hi there! *CORRECT*
* fmt
* This program is incorrect; it imports the wrong package or there isn't a function called `Println`
> 2. import "fmt" imports the `fmt` package; so you can use its functionalities.
> 3. Actually, this program is correct.

View File

@ -0,0 +1,43 @@
## What's the difference between `go build` and `go run`?
1. `go run` just compiles a program; whereas `go build` both compiles and runs it.
2. `go run` both compiles and runs a program; whereas `go build` just compiles it. *CORRECT*
> 1. It's opposite actually.
> 2. `go run` compiles your program and puts it in a temporary directory. Then it runs the compiled program in there.
## Which directory `go build` puts the compiled code into?
1. The same directory where you call `go build` *CORRECT*
2. $GOPATH/src directory
3. $GOPATH/pkg directory
4. Into a temporary directory.
> 2. There only lives Go source-code files
> 3. Go only puts your code there when you call `go install`.
## Which directory `go run` puts the compiled code into?
1. The same directory where you call `go run`
2. $GOPATH/src directory
3. $GOPATH/pkg directory
4. Into a temporary directory. *CORRECT*
## Which one below is true for runtime?
1. It happens when your program starts running in a computer *CORRECT*
2. It happens while your program is being compiled
## Which one below is true for the compile-time?
1. It happens when your program starts running in a computer
2. It happens while your program is being compiled *CORRECT*
## In which stage your program can print a message to the console?
1. While it's being compiled.
2. While it runs (after compile-time). *CORRECT*
3. While it runs (inside the compile-time).
> 1. In the compilation step your program cannot print a message. In that stage, it's literally dead.
> 2. That's right. That's the only time which your program can interact with a computer and instruct it to print a message to the console.
> 3. Running can only happen after the compile-time

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.

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() {
fmt.Println("Hello!")
// Statements change the execution flow
// Especially the control flow statements like `if`
if 5 > 1 {
fmt.Println("bigger")
}
}

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() {
fmt.Println("Hello"); fmt.Println("World!")
}

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() {
fmt.Println("Hello!" + "!")
}

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"
"runtime"
)
func main() {
// runtime.NumCPU() is a call expression
fmt.Println(runtime.NumCPU() + 1)
}

View 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 makes this package an executable program
package main
import "fmt"
/*
main function
Go executes this program using this function.
There should be only one main file in a main package.
Executable programs are also called as "commands".
*/
func main() {
fmt.Println("Hello Gopher!")
}

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
// ---------------------------------------------------------
// EXERCISE
// 1. Greet a few people
// 2. Try to type your statements separating them using
// semicolons
// 3. Observe how Go fixes them for you
// ---------------------------------------------------------
func main() {
}

View 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
func main() {
// uncomment the below line of code; then save the file
//
// you will see that Go will fix it automatically
//
// this is because, for Go, it doesn't matter whether
// you use semicolons between statements or not.
//
// it adds them automatically after all.
// fmt.Println("inanc"); fmt.Println("lina"); fmt.Println("ebru");
}

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
// ---------------------------------------------------------
// EXERCISE
// Try to type just "Hello" on a line.
// Do not use Println
// Observe the error
// ---------------------------------------------------------
func main() {
// ?
}

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
func main() {
// uncomment to see the error
// "Hello"
// It says "evaluted but not used" because
// the "Hello" expression returned a value
// and no statement has used it
// You can't use expressions without statements
}

View 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
// Print the expected output using operators
//
// EXPECTED OUTPUT
// "Hello!!!?"
// ---------------------------------------------------------
func main() {
// use + operator below multiple times
// 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"
func main() {
// Operators bind multiple expressions together
// as if there's a single expression
fmt.Println("Hello!" + "!" + "!" + "?")
}

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 runtime package documentation
// 2- Find the func that returns the Go version
// 3- Print the Go version by calling that func
//
// HINT
// It's here: https://golang.org/pkg/runtime
//
// EXPECTED OUTPUT
// "go1.10"
// ---------------------------------------------------------
func main() {
// ?
}

View 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"
"runtime"
)
func main() {
fmt.Println(runtime.Version())
}

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"
// ---------------------------------------------------------
// EXERCISE
// Use single and multiline comments to comment Printlns
//
// EXPECTED OUTPUT
// None
// ---------------------------------------------------------
func main() {
fmt.Println("hello")
fmt.Println("how")
fmt.Println("are")
fmt.Println("you")
}

View 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() {
// fmt.Println("hello")
/*
fmt.Println("how")
fmt.Println("are")
fmt.Println("you")
*/
}

View File

@ -0,0 +1,10 @@
// ---------------------------------------------------------
// EXERCISE
// 1- Print the documentation of runtime.NumCPU function
// in the command line
//
// 2- Print also its source code using in the command line
//
// HINT
// You should use correct go doc tools
// ---------------------------------------------------------

View File

@ -0,0 +1,7 @@
## DOCUMENTATION:
go doc runtime NumCPU
## SOURCE CODE:
godoc -src runtime NumCPU

View File

@ -0,0 +1,93 @@
## Which one is the correct description for a statement?
1. A statement instructs Go to do something *CORRECT*
2. A statement produces a value
3. A statement can't change the execution flow
> 2. A statement can't produce a value. However, it can indirectly help producing a value.
> 3. It surely can.
## What's the direction of execution in a Go code?
1. From left to right
2. From top to bottom *CORRECT*
3. From right to left
4. From bottom to top
> 2. That's right. Go executes the code from top-to-bottom, one statement at a time.
## Which one is the correct description for an expression?
1. An expression instructs Go to do something
2. An expression produces a value *CORRECT*
3. An expression can change the execution flow
> 1. It can't. Only a statement can do that.
> 3. It can't. Only a statement can do that.
## Which one is the correct description for an operator?
1. An operator instructs Go to do something
2. An operator can change the execution flow
3. An operator can combine expressions *CORRECT*
> 1. It can't. Only a statement can do that.
> 2. It can't. Only a statement can do that.
## Why the following code doesn't work?
```go
package main
import "fmt"
func main() {
"Hello"
}
```
1. "Hello" is an expression and it can't be on its own on a single line of code without a statement. *CORRECT*
2. By removing the double-quotes surrounding the "Hello". Like this: Hello
3. By moving "Hello" out of the func main.
## Does the following code works? And why?
```go
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.NumCPU()); fmt.Println("cpus"); fmt.Println("the machine")
}
```
1. It works: Expressions can be typed by separating them using semicolons
2. It doesn't work: Statements should be on their own on a single line of code
3. It works: Go adds semicolons behind the scenes for every statement already *CORRECT*
> 1. It works but that's not the reason. And, expressions can't be typed like that.
> 2. Are you sure?
> 3. That's right. Whether there's a semicolon or not; Go adds them automatically. Those statements are still assumed as they're on a different code line of their own.
## Why this code works?
```go
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.NumCPU() + 10)
}
```
1. Operators can combine expressions *CORRECT*
2. Statements can be used with operators
3. Expressions can return multiple values
> 1. That's right. + operator combines `runtime.NumCPU()` and `10` expressions.
> 2. No, they can't be. For example, you can't do this: `import "fmt" + 3`. Some statement can allow expressions. However, this doesn't mean that they can be combined using expressions.
> 3. That's right however it's irrelevant to why this code works.

View File

@ -0,0 +1 @@
## Please check out the questions inside the statements directory.

View File

@ -0,0 +1,68 @@
## Why do you need to use comments sometimes?
1. To combine different expressions together
2. To provide explanations or generating automatic documentation for your code *CORRECT*
3. To make the code look nice and beautiful
## Which of the following code is correct?
1.
```go
package main
/ main function is an entry point /
func main() {
fmt.Println("Hi")
}
```
2. *CORRECT*
```go
package main
// main function is an entry point /*
func main() {
fmt.Println(/* this will print Hi! */ "Hi")
}
```
3.
```go
package main
/*
main function is an entry point
It allows Go to find where to start executing an executable program.
*/
func main() {
fmt.Println(// "this will print Hi!")
}
```
> 1. `/` is not a comment. It should be `//`.
> 2. Multiline comments can be put almost anywhere. However, when a comment starts with `/*`, it also needs to end with `*/`. Here, Go doesn't interpret `/* ... */`, it just skips it. And, when Go sees `//` as the first two characters in a code, it skips the whole line.
> 3. `//` prevents Go to interpret the rest of the code line. That's why this code doesn't work. Go can't interpret this part because of the comment: `"this will print Hi!")`
## How should you name your code so that Go can generate documentation from your code automatically?
1. By commenting the each line of the code; then it will generate the documentation from whatever it sees
2. By starting the comments using the name of the declared names *CORRECT*
3. By using multi-line comments
> 1. This won't help. Sorry.
> 3. It doesn't matter whether you type your comments using single-line comments or multi-line comments.
## Which tool do you need to use from the command-line to print the documentation?
1. go build
2. go run
3. go doctor
4. go doc
## What's the difference between `godoc` and `go doc`?
1. `go doc` is the real tool behind `godoc`
2. `godoc` is the real tool behind `go doc` *CORRECT*
3. `go` tool is the real tool behind `go doc`
4. `go` tool is the real tool behind `godoc`
> 2. That's right. go doc tool uses godoc tool behind the scenes. go doc is just a simplified version of the godoc tool.

View File

@ -0,0 +1,18 @@
# EXERCISE
1. Create a new library
2. In it, create a function that returns Go version
3. Create a command and import your library
4. Call your function that returns Go version
5. Run your program
## HINTS
**Create your package function like this:**
```go
func Version() string {
return runtime.Version()
}
```
## EXPECTED OUTPUT
It should print the current Go version on your system.

View File

@ -0,0 +1,90 @@
## Which one below is correct?
**NOTE** _There are explanations inside the answers. Even if you know the answer please try to select all of them one by one, so you can read the explanations._
1. You can run a library package.
2. In a library package there should be a function named main (func main).
3. You can compile a library package. *CORRECT*
4. You have to compile a library package.
> 1. You can't, but you can import it from other packages.
> 2. In a library package, you don't have to include a main function. Because, it isn't an executable package. Only in executable packages you need a main func.
> 4. You don't have to compile it. When you import it, it will automatically be built by the other program or library when it gets compiled or ran.
## What do you need to export a name?
1. You need to type it in all capital letters
2. You need to type its first letter as a capital letter *CORRECT*
3. You need to put it inside a function scope
4. You need to create a new file for that name
> 1. When you do so, it will be exported, that's true, but don't do that; so I assume that this answer is not correct :)
> 2. That's right. Then the other packages can access it.
> 3. It should be in a package scope, not function scope.
> 4. You don't have to do that.
## How can you use a function from your library from an executable program?
1. You need to export your library package first; then you can access to its imported names
2. You need to import your library package first; then you can access to its exported names *CORRECT*
3. You can access your library package as if it's in your executable program
4. You can import it just by using its name
> 1. You can't export packages. All packages are already exported. Unless you put them in a directory called: "internal". But, that's an advanced topic for now.
> 2. That's right.
> 3. You can't access to a package from another package without importing it.
> 4. No, you can't. You need to import it using its full directory path after GOPATH. BTW, in the near future this may change with the Go modules support.
## In the following program, which names are exported?
```go
package wizard
import "fmt"
func doMagic() {
fmt.Println("enchanted!")
}
func Fireball() {
fmt.Println("fireball!!!")
}
```
1. fmt
2. doMagic
3. Fireball *CORRECT*
4. Println
> 1. That's just an imported package name.
> 2. It starts with a lowercase letter; so, it's not exported.
> 3. That's right. It starts with a capital letter.
> 4. This isn't your function. It's already been exported in the fmt package. But, it isn't exported here.
## In the following program, which names are exported?
```go
package wizard
import "fmt"
var one string
var Two string
var greenTrees string
func doMagic() {
fmt.Println("enchanted!")
}
func Fireball() {
fmt.Println("fireball!!!")
}
```
1. doMagic and Fireball
2. Fireball and Two *CORRECT*
3. Fireball, greenTrees and Two
4. Fireball, greenTrees, one and Two
> 1. doMagic starts with a lowercase letter; so, it's not exported.
> 2. That's right. Fireball and Two, they both start with a capital letter.
> 3. greenTrees starts with a lowercase letter; so, it's not exported.
> 4. one and greenTrees do not start with capital letters; so, they're not exported.

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" // You should replace this with your username
"github.com/inancgumus/learngo/05-write-your-first-library-package/printer-exercise/solution/golang"
)
func main() {
fmt.Println(golang.Version())
}

View 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 golang
import (
"runtime"
)
// Version returns the current Go version
func Version() string {
return runtime.Version()
}

View 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
// Automatically imports!... AWESOME!
import "github.com/inancgumus/learngo/05-write-your-first-library-package/printer"
func main() {
printer.Hello()
}

View 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 printer
import "fmt"
// Hello is an exported function
func Hello() {
fmt.Println("exported hello")
}

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

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() {
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ç!")
}

View 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
}

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(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
}

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() {
// 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!"
)
}

View 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

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() {
var speed int
fmt.Println(speed)
}

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
// 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, 速度
}

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() {
// fmt.Println(speed)
// var speed int
}

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 nFiles int
var counter int
var nCPU int
fmt.Println(
nFiles,
counter,
nCPU,
)
}

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 heat float64
var ratio float64
var degree float64
fmt.Println(
heat,
ratio,
degree,
)
}

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 off bool
var valid bool
var closed bool
fmt.Println(
off,
valid,
closed,
)
}

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 msg string
var name string
var text string
fmt.Println(
msg,
name,
text,
)
}

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"
// 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)
}

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
// 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)
}

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
func main() {
var speed int
// let's assign the variable to the blank-identifier
// so that Go compiler won't get grumpy
_ = speed
}

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() {
var (
speed int
heat float64
off bool
brand string
)
fmt.Println(speed)
fmt.Println(heat)
fmt.Println(off)
fmt.Printf("%q\n", brand)
}

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 is equal to:
//
// var (
// speed int
// velocity int
// )
//
// or:
//
// var speed int
// var velocity int
//
var speed, velocity int
fmt.Println(speed, velocity)
}

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() {
// 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)
}

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
// ---------------------------------------------------------
// 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 ? ?
// ?
}

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() {
var height int
fmt.Println(height)
}

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
// ---------------------------------------------------------
// OPTIONAL EXERCISE
// 1- Declare and print a bool variable
// 2- The variable's name should be: isOn
//
// EXPECTED OUTPUT
// false
// ---------------------------------------------------------
func main() {
// var ? ?
// ?
}

View 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)
}

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
// ---------------------------------------------------------
// 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 ? ?
// ?
}

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() {
var brightness int
fmt.Println(brightness)
}

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
// ---------------------------------------------------------
// 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
}

View 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)
}

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- 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
}

View 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
}

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
// ---------------------------------------------------------
// 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....
}

View 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)
}

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. 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)
}

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() {
var (
active bool
delta int
)
fmt.Println(active, delta)
}

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. 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", ?, ?)
}

View 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)
}

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

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() {
var isLiquid bool
_ = isLiquid
}

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
// ---------------------------------------------------------
// EXERCISE
// 1- Declare a variable in the package-scope
//
// 2- Observe whether something happens when you don't
// use it
// ---------------------------------------------------------
func main() {
}

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
var isLiquid bool
func main() {
}

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
// ---------------------------------------------------------
// 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 ? ?
}

Some files were not shown because too many files have changed in this diff Show More