Initial commit
This commit is contained in:
32
06-variables/06-project-greeter/01-demonstration/main.go
Normal file
32
06-variables/06-project-greeter/01-demonstration/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// NOTE: RUN THIS WITH 3 ARGUMENTS AT LEAST
|
||||
// OR, THERE WILL BE AN ERROR
|
||||
|
||||
func main() {
|
||||
fmt.Printf("%#v\n", os.Args)
|
||||
|
||||
// Gets an item from the os.Args string slice:
|
||||
// os.Args[INDEX]
|
||||
// INDEX can be 0 or greater
|
||||
fmt.Println("Path:", os.Args[0])
|
||||
fmt.Println("1st argument:", os.Args[1])
|
||||
fmt.Println("2nd argument:", os.Args[2])
|
||||
fmt.Println("3rd argument:", os.Args[3])
|
||||
|
||||
// `len` function can find how many items
|
||||
// inside a slice value
|
||||
fmt.Println("Items inside os.Args:", len(os.Args))
|
||||
}
|
||||
35
06-variables/06-project-greeter/02-version1/main.go
Normal file
35
06-variables/06-project-greeter/02-version1/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Since, you didn't learn about the control flow statements yet
|
||||
// I didn't include an error detection here.
|
||||
//
|
||||
// So, if you don't pass a name and a lastname,
|
||||
// this program will fail.
|
||||
// This is intentional.
|
||||
|
||||
func main() {
|
||||
var name string
|
||||
|
||||
// assign a new value to the string variable below
|
||||
name = os.Args[1]
|
||||
fmt.Println("Hello great", name, "!")
|
||||
|
||||
// changes the name, declares the age with 85
|
||||
name, age := "gandalf", 2019
|
||||
|
||||
fmt.Println("My name is", name)
|
||||
fmt.Println("My age is", age)
|
||||
fmt.Println("BTW, you shall pass!")
|
||||
}
|
||||
33
06-variables/06-project-greeter/03-version2/main.go
Normal file
33
06-variables/06-project-greeter/03-version2/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Since, you didn't learn about the control flow statements yet
|
||||
// I didn't include an error detection here.
|
||||
//
|
||||
// So, if you don't pass a name and a lastname,
|
||||
// this program will fail.
|
||||
// This is intentional.
|
||||
|
||||
func main() {
|
||||
// assign a new value to the string variable below
|
||||
name := os.Args[1]
|
||||
fmt.Println("Hello great", name, "!")
|
||||
|
||||
// changes the name, declares the age with 85
|
||||
name, age := "gandalf", 2019
|
||||
|
||||
fmt.Println("My name is", name)
|
||||
fmt.Println("My age is", age)
|
||||
fmt.Println("BTW, you shall pass!")
|
||||
}
|
||||
27
06-variables/06-project-greeter/exercises/01/main.go
Normal file
27
06-variables/06-project-greeter/exercises/01/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Print the count of the command-line arguments
|
||||
//
|
||||
// INPUT
|
||||
// bilbo balbo bungo
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// There are 3 names.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// FIX THIS CODE
|
||||
// count := ?
|
||||
|
||||
// DO NOT TOUCH THIS CODE
|
||||
// fmt.Printf("There are %d names.\n", count)
|
||||
}
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
count := len(os.Args) - 1
|
||||
|
||||
fmt.Printf("There are %d names.\n", count)
|
||||
}
|
||||
24
06-variables/06-project-greeter/exercises/02/main.go
Normal file
24
06-variables/06-project-greeter/exercises/02/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Print the path of the running program
|
||||
// By getting it from `os.Args` variable
|
||||
//
|
||||
// HINT
|
||||
// Use `go build` to build your program.
|
||||
// Then run it using the compiled executable program file.
|
||||
//
|
||||
// EXPECTED OUTPUT SHOULD INCLUDE THIS
|
||||
// myprogram
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
||||
@@ -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"
|
||||
"os"
|
||||
)
|
||||
|
||||
// STEPS:
|
||||
//
|
||||
// Compile it by typing:
|
||||
// go build -o myprogram
|
||||
//
|
||||
// Then run it by typing:
|
||||
// ./myprogram
|
||||
//
|
||||
// If you're on Windows, then type:
|
||||
// myprogram
|
||||
|
||||
func main() {
|
||||
fmt.Println(os.Args[0])
|
||||
}
|
||||
34
06-variables/06-project-greeter/exercises/03/main.go
Normal file
34
06-variables/06-project-greeter/exercises/03/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Print your name
|
||||
// Get it from the first command-line argument
|
||||
//
|
||||
// INPUT
|
||||
// Call the program using your name
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// It should print your name
|
||||
//
|
||||
// EXAMPLE
|
||||
// go run main.go inanc
|
||||
// inanc
|
||||
//
|
||||
// BONUS: Make the output like this:
|
||||
// go run main.go inanc
|
||||
// Hi inanc
|
||||
// How are you?
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// get your name from the command-line
|
||||
// and print it
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(os.Args[1])
|
||||
|
||||
// BONUS SOLUTION:
|
||||
fmt.Println("Hello", os.Args[1])
|
||||
fmt.Println("How are you?")
|
||||
}
|
||||
34
06-variables/06-project-greeter/exercises/04/main.go
Normal file
34
06-variables/06-project-greeter/exercises/04/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Greet more people
|
||||
//
|
||||
// RESTRICTIONS
|
||||
// Be sure to match to the expected output below
|
||||
//
|
||||
// INPUT
|
||||
// bilbo balbo bungo
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// There are 3 people!
|
||||
// Hello great bilbo !
|
||||
// Hello great balbo !
|
||||
// Hello great bungo !
|
||||
// Nice to meet you all.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// TYPE YOUR CODE HERE
|
||||
|
||||
// BONUS #1:
|
||||
// Observe the error if you pass less then 3 arguments.
|
||||
// Search on the web how to solve that.
|
||||
}
|
||||
@@ -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"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
l = len(os.Args) - 1
|
||||
n1 = os.Args[1]
|
||||
n2 = os.Args[2]
|
||||
n3 = os.Args[3]
|
||||
)
|
||||
|
||||
fmt.Println("There are", l, "people !")
|
||||
fmt.Println("Hello great", n1, "!")
|
||||
fmt.Println("Hello great", n2, "!")
|
||||
fmt.Println("Hello great", n3, "!")
|
||||
fmt.Println("Nice to meet you all.")
|
||||
}
|
||||
34
06-variables/06-project-greeter/exercises/05/main.go
Normal file
34
06-variables/06-project-greeter/exercises/05/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE
|
||||
// Greeat 5 people this time.
|
||||
// Do not copy paste from the previous exercise!
|
||||
// And do not look at it.
|
||||
//
|
||||
// RESTRICTION
|
||||
// This time do not use variables.
|
||||
//
|
||||
// INPUT
|
||||
// bilbo balbo bungo gandalf legolas
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// There are 5 people!
|
||||
// Hello great bilbo !
|
||||
// Hello great balbo !
|
||||
// Hello great bungo !
|
||||
// Hello great gandalf !
|
||||
// Hello great legolas !
|
||||
// Nice to meet you all.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// TYPE YOUR CODE HERE
|
||||
}
|
||||
@@ -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"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("There are", len(os.Args)-1, "people !")
|
||||
fmt.Println("Hello great", os.Args[1], "!")
|
||||
fmt.Println("Hello great", os.Args[2], "!")
|
||||
fmt.Println("Hello great", os.Args[3], "!")
|
||||
fmt.Println("Hello great", os.Args[4], "!")
|
||||
fmt.Println("Hello great", os.Args[5], "!")
|
||||
fmt.Println("Nice to meet you all.")
|
||||
}
|
||||
@@ -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"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// This is a solution to the exercise that I asked
|
||||
// in the lecture.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
// NOTE: You should run this program with 3 arguments
|
||||
// at least. Or there will be an error.
|
||||
|
||||
func main() {
|
||||
// assign a new value to the string variable below
|
||||
var (
|
||||
name = os.Args[1]
|
||||
name2 = os.Args[2]
|
||||
name3 = os.Args[3]
|
||||
)
|
||||
|
||||
fmt.Println("Hello great", name, "!")
|
||||
fmt.Println("And hellooo to you magnificient", name2, "!")
|
||||
fmt.Println("Welcome", name3, "!")
|
||||
|
||||
// changes the name, declares the age with 131
|
||||
name, age := "bilbo baggins", 131 // unknown age!
|
||||
|
||||
fmt.Println("My name is", name)
|
||||
fmt.Println("My age is", age)
|
||||
fmt.Println("And, I love adventures!")
|
||||
}
|
||||
54
06-variables/06-project-greeter/questions/questions.md
Normal file
54
06-variables/06-project-greeter/questions/questions.md
Normal file
@@ -0,0 +1,54 @@
|
||||
## What's does `os.Args` variable store in its first item?
|
||||
* The first argument that is passed to the program
|
||||
* The second argument that is passed to the program
|
||||
* Path to the running program *CORRECT*
|
||||
|
||||
## What's the type of the `Args` variable?
|
||||
```go
|
||||
var Args []string
|
||||
```
|
||||
* string
|
||||
* string array
|
||||
* a slice of strings *CORRECT*
|
||||
|
||||
## What is the type of each value in the `Args` variable?
|
||||
```go
|
||||
var Args []string
|
||||
```
|
||||
* string *CORRECT*
|
||||
* string array
|
||||
* a slice of strings
|
||||
|
||||
## How to get the first item of the `Args` variable?
|
||||
```go
|
||||
var Args []string
|
||||
```
|
||||
* Args.0
|
||||
* Args{1}
|
||||
* Args[0] *CORRECT*
|
||||
* Args(1)
|
||||
|
||||
## How to get the second item of the `Args` variable?
|
||||
```go
|
||||
var Args []string
|
||||
```
|
||||
* Args.2
|
||||
* Args[1] *CORRECT*
|
||||
* Args{1}
|
||||
* Args(2)
|
||||
|
||||
## How to get the length of the `Args` variable?
|
||||
```go
|
||||
var Args []string
|
||||
```
|
||||
* length(Args)
|
||||
* Args.len
|
||||
* len(Args) *CORRECT*
|
||||
* Args.Length
|
||||
|
||||
## How to get the first "argument" from the command-line?
|
||||
* os.Args[0]
|
||||
* os.Args[1] *CORRECT*
|
||||
* os.Args[2]
|
||||
* os.Args[3]
|
||||
|
||||
Reference in New Issue
Block a user