Skip to content

Commit 5e0458f

Browse files
committed
Day 11
1 parent 02f9d4c commit 5e0458f

11 files changed

+206
-2
lines changed

Days/Go/day11_example1.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var challenge = "#90DaysOfDevOps"
7+
fmt.Println("Welcome to", challenge, "")
8+
}

Days/Go/day11_example2.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var challenge = "#90DaysOfDevOps"
7+
const daystotal = 90
8+
9+
fmt.Println("Welcome to", challenge)
10+
fmt.Println("This is a", daystotal, "challenge")
11+
}

Days/Go/day11_example3.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var challenge = "#90DaysOfDevOps"
7+
const daystotal = 90
8+
var dayscomplete = 11
9+
10+
fmt.Println("Welcome to", challenge, "")
11+
fmt.Println("This is a", daystotal, "challenge and you have completed", dayscomplete, "days")
12+
fmt.Println("Great work")
13+
}

Days/Go/day11_example4.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var challenge = "#90DaysOfDevOps"
7+
const daystotal = 90
8+
var dayscomplete = 11
9+
10+
fmt.Printf("Welcome to %v\n", challenge)
11+
fmt.Printf("This is a %v challenge and you have completed %v days\n", daystotal, dayscomplete)
12+
fmt.Println("Great work")
13+
}

Days/Images/Day11_Go1.png

26.8 KB
Loading

Days/Images/Day11_Go2.png

15.8 KB
Loading

Days/Images/Day11_Go3.png

17.8 KB
Loading

Days/day08.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ go build main.go
9090

9191
See you on [Day 9](day09.md).
9292

93-
![](images\Day8_Go13.png)
93+
![](images/Day8_Go13.png)

Days/day11.md

+155
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,159 @@
1+
Before we get into the topics for today I want to give a massive shout out to [Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I) and this fantastic concise journey through the fundamentals of Go.
2+
3+
On [Day8](day08.md) we set our environment up, on [Day9](day09.md) we walked through the Hello #90DaysOfDevOps code and on [Day10](day10.md)) we looked at our Go workspace and went into a little more deeper into compiling and running the code.
4+
5+
Today we are going to take a look into Variables, Constants and Data Types whilst writing a new program.
6+
17
## Variables & Constants in Go
8+
Let's start by planning our application, I think it would be a good idea to work on a program that tells us how many days we have remaining in our #90DaysOfDevOps challenge.
9+
10+
The first thing to consider here is that as we are building our app and we are welcoming our attendees and we are giving the user feedback on the amount of days they have completed we might use the term #90DaysOfDevOps a number of times throughout the program. This is a great use case to make #90DaysOfDevOps a variable within our program.
11+
12+
- Variables are used to store values.
13+
- Like a little box with our saved information or values.
14+
- We can then use this variable across the program which also benefits that if this challenge or variable changes then we only have to change this in one place. Meaning we could translate this to other challenges we have in the community by just changing that one variable value.
15+
16+
In order to declare this in our Go Program we define a value by using a **keyword** for variables. This will live within our `func main` block of code that you will see later. You can find more about [Keywords](https://go.dev/ref/spec#Keywords)here.
17+
18+
Remember to make sure that your variable names are descriptive. If you declare a variable you must use it or you will get an error, this is to avoid possible dead code, code that is never used. This is the same for packages not used.
19+
20+
```
21+
var challenge = "#90DaysOfDevOps"
22+
```
23+
With the above set and used as we will see in the next code snippet you can see from the output below that we have used a variable.
24+
25+
```
26+
package main
27+
28+
import "fmt"
29+
30+
func main() {
31+
var challenge = "#90DaysOfDevOps"
32+
fmt.Println("Welcome to", challenge "")
33+
}
34+
```
35+
You can find the above code snippet in [day11_example1.go](Go/day11_example1.go)
36+
37+
You will then see from the below that we built our code with the above example and we got the output shown below.
38+
39+
![](Images/Day11_Go1.png)
40+
41+
We also know that our challenge is 90 days at least for this challenge, but next maybe its 100 so we want to define a variable to help us here as well. However for the purpose of our program we want to define this as a constant. Constants are like variables, except that their value cannot be changed within code (we can still create a new app later on down the line with this code and change this constant but this 90 will not change whilst we are running our application)
42+
43+
Adding the `const` to our code and adding another line of code to print this.
44+
45+
```
46+
package main
47+
48+
import "fmt"
49+
50+
func main() {
51+
var challenge = "#90DaysOfDevOps"
52+
const daystotal = 90
53+
54+
fmt.Println("Welcome to", challenge)
55+
fmt.Println("This is a", daystotal, "challenge")
56+
}
57+
```
58+
You can find the above code snippet in [day11_example2.go](Go/day11_example2.go)
59+
60+
If we then go through that `go build` process again and run you will see below the outcome.
61+
62+
![](Images/Day11_Go2.png)
63+
64+
Finally, and this won't be the end of our program we will come back to this in [Day12](day12.md) to add more functionality. We now want to add another variable for the number of days we have completed of the challenge.
65+
66+
Below I added `dayscomplete` variable with the amount of days completed.
67+
68+
```
69+
package main
70+
71+
import "fmt"
72+
73+
func main() {
74+
var challenge = "#90DaysOfDevOps"
75+
const daystotal = 90
76+
var dayscomplete = 11
77+
78+
fmt.Println("Welcome to", challenge, "")
79+
fmt.Println("This is a", daystotal, "challenge and you have completed", dayscomplete, "days")
80+
fmt.Println("Great work")
81+
}
82+
```
83+
You can find the above code snippet in [day11_example3.go](Go/day11_example3.go)
84+
85+
Let's run through that `go build` process again or you could just use `go run`
86+
87+
![](Images/Day11_Go3.png)
88+
89+
Here are some other examples that I have used to make the code easier to read and edit. We have up till now been using `Println` but we can simplify this by using `Printf` by using `%v` which means we define our variables in order at the end of the line of code. we also use `\n` for a line break.
90+
91+
I am using `%v` as this uses a default value but there are other options that can be found here in the [fmt package documentation](https://pkg.go.dev/fmt) you can find the code example [day11_example4.go](Go/day11_example4.go)
92+
93+
Variables may also be defined in a simpler format in your code. Instead of defining that it is a `var` and the `type` you can code this as follows to get the same functionality but a nice cleaner and simpler look for your code. This will only work for variables though and not constants.
94+
95+
```
96+
func main() {
97+
challenge := "#90DaysOfDevOps"
98+
const daystotal = 90
99+
```
2100

3101
## Data Types
102+
In the above examples we have not defined the type of variables, this is because we can give it a value here and Go is smart enough to know what that type is or at least can infer what it is based on the value you have stored. However if we want user input this will require a specific type.
103+
104+
We have used Strings and Integers in our code so far. Integers for the number of days and strings are for the name of the challenge.
105+
106+
It is also important to note that each data type can do different things and behaves differently. For example integers can multiply where as strings do not.
107+
108+
There are four categories
109+
110+
- **Basic type**: Numbers, strings, and booleans come under this category.
111+
- **Aggregate type**: Array and structs come under this category.
112+
- **Reference type**: Pointers, slices, maps, functions, and channels come under this category.
113+
- **Interface type**
114+
115+
Data type is an important concept in programming. Data type specifies the size and type of variable values.
116+
117+
Go is statically typed, meaning that once a variable type is defined, it can only store data of that type.
118+
119+
Go has three basic data types:
120+
121+
- **bool**: represents a boolean value and is either true or false
122+
- **Numeric**: represents integer types, floating point values, and complex types
123+
- **string**: represents a string value
124+
125+
I found this resource super detailed on data types [Golang by example](https://golangbyexample.com/all-data-types-in-golang-with-examples/)
126+
127+
I would also suggest [Techworld with Nana](https://www.youtube.com/watch?v=yyUHQIec83I&t=2023s) at this point covers in some detail a lot about the data types in Go.
128+
129+
If we need to define a type in our variable we can do this like so:
130+
131+
```
132+
var TwitterHandle string
133+
var DaysCompleted uint
134+
```
135+
136+
Because Go implies variables where a value is given we can print out those values with the following:
137+
138+
```
139+
fmt.Printf("challenge is %T, daystotal is %T, dayscomplete is %T\n", conference, daystotal, dayscomplete)
140+
```
141+
There are many different types of integer and float types the links above will cover off these in detail.
142+
143+
- **int** = whole numbers
144+
- **unint** = positive whole numbers
145+
- **floating point types** = numbers that contain a decimal component
146+
147+
## Resources
148+
149+
- [StackOverflow 2021 Developer Survey](https://insights.stackoverflow.com/survey/2021)
150+
- [Why we are choosing Golang to learn](https://www.youtube.com/watch?v=7pLqIIAqZD4&t=9s)
151+
- [Jake Wright - Learn Go in 12 minutes](https://www.youtube.com/watch?v=C8LgvuEBraI&t=312s)
152+
- [Techworld with Nana - Golang full course - 3 hours 24 mins](https://www.youtube.com/watch?v=yyUHQIec83I)
153+
- [**NOT FREE** Nigel Poulton Pluralsight - Go Fundamentals - 3 hours 26 mins](https://www.pluralsight.com/courses/go-fundamentals)
154+
- [FreeCodeCamp - Learn Go Programming - Golang Tutorial for Beginners](https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1025s)
155+
- [Hitesh Choudhary - Complete playlist](https://www.youtube.com/playlist?list=PLRAV69dS1uWSR89FRQGZ6q9BR2b44Tr9N)
156+
157+
Next up we are going to start adding some user input functionality to our program so that we are asking how many days have been completed.
4158

159+
See you on [Day 12](day12.md).

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This will not cover all things DevOps but it will cover the areas that I feel wi
2626
- [ ] ⌨️ 8 > [Setting up your DevOps environment for Go & Hello World](Days/day08.md)
2727
- [ ] ⌨️ 9 > [Let's explain the Hello World code](Days/day09.md)
2828
- [ ] ⌨️ 10 > [The Go Workspace & Compiling & running code](Days/day10.md)
29-
- [ ] ⌨️ 11 > [](Days/day11.md)
29+
- [ ] ⌨️ 11 > [Variables, Constants & Data Types](Days/day11.md)
3030
- [ ] ⌨️ 12 > [](Days/day12.md)
3131
- [ ] ⌨️ 13 > [](Days/day13.md)
3232

Resources.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Resources
2+
3+
4+
https://github.com/bregman-arie/devops-exercises

0 commit comments

Comments
 (0)