This commit is contained in:
michaelcade 2022-01-12 08:00:24 +00:00
parent 5e0458f95a
commit aa09738cac
11 changed files with 156 additions and 14 deletions

21
Days/Go/day12_example1.go Normal file
View File

@ -0,0 +1,21 @@
package main
import "fmt"
func main() {
challenge := "#90DaysOfDevOps"
const daystotal = 90
fmt.Printf("Welcome to %v\n", challenge)
fmt.Printf("This is a %v challenge\n", daystotal)
var TwitterName string
var DaysComplete int
// ask user for their twitter handle
TwitterName = "@MichaelCade1"
DaysComplete = 12
fmt.Printf("%v has completed %v days of the challenge\n", TwitterName, DaysComplete)
fmt.Println("Great work")
}

24
Days/Go/day12_example2.go Normal file
View File

@ -0,0 +1,24 @@
package main
import "fmt"
func main() {
const DaysTotal int = 90
challenge := "#90DaysOfDevOps"
fmt.Printf("Welcome to the %v challenge.\nThis challenge consists of %v days\n", challenge, DaysTotal)
var TwitterName string
var DaysCompleted uint
// asking for user input
fmt.Println("Enter Your Twitter Handle: ")
fmt.Scanln(&TwitterName)
fmt.Println("How many days have you completed?: ")
fmt.Scanln(&DaysCompleted)
fmt.Printf("Thank you %v for taking part and completing %v days.\n", TwitterName, DaysCompleted)
fmt.Println("Good luck")
}

29
Days/Go/day12_example3.go Normal file
View File

@ -0,0 +1,29 @@
package main
import "fmt"
func main() {
const DaysTotal int = 90
var remainingDays uint = 90
challenge := "#90DaysOfDevOps"
fmt.Printf("Welcome to the %v challenge.\nThis challenge consists of %v days\n", challenge, DaysTotal)
var TwitterName string
var DaysCompleted uint
// asking for user input
fmt.Println("Enter Your Twitter Handle: ")
fmt.Scanln(&TwitterName)
fmt.Println("How many days have you completed?: ")
fmt.Scanln(&DaysCompleted)
// calculate remaining days
remainingDays = remainingDays - DaysCompleted
fmt.Printf("Thank you %v for taking part and completing %v days.\n", TwitterName, DaysCompleted)
fmt.Printf("You have %v days remaining for the %v challenge\n", remainingDays, challenge)
fmt.Println("Good luck")
}

11
Days/Go/day12_example4.go Normal file
View File

@ -0,0 +1,11 @@
package main
import "fmt"
func main() {
var challenge = "#90DaysOfDevOps"
fmt.Println(challenge)
fmt.Println(&challenge)
}

BIN
Days/Images/Day12_Go1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
Days/Images/Day12_Go2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
Days/Images/Day12_Go3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
Days/Images/Day12_Go4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -1,4 +1,74 @@
## Getting user input with Pointers and a finished program
Yesterday ([Day 11](day11.md)), we created our first Go program that was self contained and the parts we wanted to really get user input for were created as variables within our code and given values, we now want to ask the user for their input to give the variable the value for the end message.
## Getting user input ## Getting user input
Before we do that let's take a look at our application again and walk through the variables we want as a test before getting that user input.
Yesterday we finished up with our code looking like this [day11_example4.go](Go/day11_example4.go) we have manually in code defined our `challenge, daystotal, dayscomplete` variables and constants.
Let's now add a new variable called `TwitterName` you can find this new code at [day12_example1.go](Go/day12_example1.go) and if we run this code this is our output.
![](Images/Day12_Go1.png)
We are on day 12 and we would need to change that `dayscomplete` every day and compile our code each day if this was hard coded which doesn't sound so great.
Getting user input, we want to get the value of maybe a name and the amount of days completed. For us to do this we can use another function from within the `fmt` package.
Recap on the `fmt` package, different functions for: formatted input and output (I/O)
- Print Messages
- Collect User Input
- Write into a file
This is instead of assigning the value of a variable we want to ask the user for their input.
```
fmt.Scan(&TwitterName)
```
Notice that we also use `&` before the variable. This is known as a pointer which we will cover in the next section.
In our code [day12_example2.go](Go/day12_example2.go) you can see that we are asking the user to input two variables, `TwitterName` and `DaysCompleted`
Let's now run our program and you see we have input for both of the above.
![](Images/Day12_Go2.png)
Ok, that's great we got some user input and we printed a message but what about getting our program to tell us how many days we have left in our challenge.
In order for us to do that we have created a variable called `remainingDays` and we have hard valued this in our code as `90` we then need to change the value of this value to print out the remaining days when we get our user input of `DaysCompleted` we can do this with this simple variable change.
```
remainingDays = remainingDays - DaysCompleted
```
You can see how our finished program looks here [day12_example2.go](Go/day12_example3.go).
If we now run this program you can see that simple calculation is made based on the user input and the value of the `remainingDays`
![](Images/Day12_Go3.png)
## What is a pointer? (Special Variables) ## What is a pointer? (Special Variables)
A pointer is a (special) variable that points to the memory address of another variable.
A great explanation of this can be found here at [geeksforgeeks](https://www.geeksforgeeks.org/pointers-in-golang/)
Let's simplify our code now and show with and without the `&` in front of one of our print commands,this gives us the memory address of the pointer. I have added this code example here. [day12_example4.go](Go/day12_example4.go)
Below is running this code.
![](Images/Day12_Go4.png)
## Resources
- [StackOverflow 2021 Developer Survey](https://insights.stackoverflow.com/survey/2021)
- [Why we are choosing Golang to learn](https://www.youtube.com/watch?v=7pLqIIAqZD4&t=9s)
- [Jake Wright - Learn Go in 12 minutes](https://www.youtube.com/watch?v=C8LgvuEBraI&t=312s)
- [Techworld with Nana - Golang full course - 3 hours 24 mins](https://www.youtube.com/watch?v=yyUHQIec83I)
- [**NOT FREE** Nigel Poulton Pluralsight - Go Fundamentals - 3 hours 26 mins](https://www.pluralsight.com/courses/go-fundamentals)
- [FreeCodeCamp - Learn Go Programming - Golang Tutorial for Beginners](https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1025s)
- [Hitesh Choudhary - Complete playlist](https://www.youtube.com/playlist?list=PLRAV69dS1uWSR89FRQGZ6q9BR2b44Tr9N)
See you on [Day 13](day13.md).

View File

@ -1,13 +0,0 @@
Arrays and Slices
Loops
Conditionals (if / else ) and boolean data types
Switch statement
Resources
- https://github.com/bregman-arie/devops-exercises
- https://gobyexample.com/ - Could be good with some examples for each Values, Variables depending on what Day 9 covers.
- https://go.dev/tour/list
- https://go.dev/learn/ - not for the whole topic but be good to highlight this

View File

@ -27,7 +27,7 @@ This will not cover all things DevOps but it will cover the areas that I feel wi
- [ ] ⌨️ 9 > [Let's explain the Hello World code](Days/day09.md) - [ ] ⌨️ 9 > [Let's explain the Hello World code](Days/day09.md)
- [ ] ⌨️ 10 > [The Go Workspace & Compiling & running code](Days/day10.md) - [ ] ⌨️ 10 > [The Go Workspace & Compiling & running code](Days/day10.md)
- [ ] ⌨️ 11 > [Variables, Constants & Data Types](Days/day11.md) - [ ] ⌨️ 11 > [Variables, Constants & Data Types](Days/day11.md)
- [ ] ⌨️ 12 > [](Days/day12.md) - [ ] ⌨️ 12 > [Getting user input with Pointers and a finished program](Days/day12.md)
- [ ] ⌨️ 13 > [](Days/day13.md) - [ ] ⌨️ 13 > [](Days/day13.md)
### Knowing Linux Basics ### Knowing Linux Basics