Skip to content

Commit 1a5a1b1

Browse files
committed
readme, renames
1 parent 9652ec6 commit 1a5a1b1

File tree

68 files changed

+867
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+867
-82
lines changed

01 basics/04 loops/README.md

-15
This file was deleted.

01 basics/05 - user input/README.md

-34
This file was deleted.

01 basics/05 - user input/main.go

-22
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

01 basics/03 if and else/README.md renamed to 01-basics/03-if-and-else/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Boolean logic
1+
Go from the beginning - applying boolean logic
22

33
> This article will cover working with boolean logic. You will learn how to work with boolean data, `if`, `else if` and `else` constructs.
44
File renamed without changes.

01 basics/04 - conversions/README.md renamed to 01-basics/04-conversions/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Converting between types
1+
# Go from the beginning - converting between types
22

33
There's different data types and a need to convert between them. For example, we often need to convert between text and numbers for presentational and other reasons. We also need to convert between numbers and decimals without loosing information in the process.
44

@@ -192,3 +192,7 @@ str := strconv.Itoa(noOfPlayers)
192192
## Additional parsing
193193

194194
The `strconv` library is what you want if you start with a string and you want to convert to and from another format. Learn more about [strconv library here](https://pkg.go.dev/strconv)
195+
196+
## Summary
197+
198+
TODO
File renamed without changes.

01-basics/05-loops/README.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Go from the beginning - working with loops
2+
3+
> TLDR; this article covers working with loops in Go
4+
5+
You are likely to want repeat a set of instructions. For example, you might have a list of orders where you need to process each order. Or you have a file where you need to read it line by line or there might be some other calculation. Regardless of your situation, you are likely to need a looping construct, so what are your options in Go?
6+
7+
Basically, you are using the `for` loop. There are three major ways yu can use it:
8+
9+
- **increment steps**. With the help of a variable, you define a start point, a condition when to stop and an incrementation step. This is your "classic" for-loop. Here's what it looks like:
10+
11+
```go
12+
for i := 0; i < 10; i++ {
13+
// run these statements
14+
}
15+
```
16+
17+
- while. In many programming languages you a have a `while` keyword. Go doesn't have that, but what you can do is to use the for-loop in a similar way. You omit the initialization step and increment step and get this code:
18+
19+
```go
20+
for <condition> {
21+
// run these statements
22+
}
23+
```
24+
25+
- for each. lastly, you have the `for-each` like construct that operates on an array like sequence. It uses the `range` keyword to function:
26+
27+
```go
28+
for i,s := range array {
29+
// run these statements
30+
}
31+
```
32+
33+
## The `for` loop
34+
35+
The conventional for-loop has three different parts:
36+
37+
- initialization, here you want to create a variable and assign it a starter value like so:
38+
39+
```go
40+
for i:= 0;
41+
```
42+
43+
Note the use of `;`, you usually don's use semicolon, but for this construct, you need it.
44+
45+
- condition. The next step is evaluating whether you should continue incrementing or not. You define a boolean expression here, that if `true`, continues to loop:
46+
47+
```go
48+
for i := 0; i< 10
49+
```
50+
51+
`i < 10`, as long as a value is between 0 and 10 (becomes 10, then loop breaks), then it returns true and the loop continues.
52+
53+
- increment, in this step, the loop variable `i` is updated, updating it by 1 is common but you can add any increment size, negative or positive.
54+
55+
```go
56+
for i := 0; i< 10; i++ {
57+
58+
}
59+
```
60+
61+
Here, `i` is updated by 1. This loop will run ten times.
62+
63+
## Repeat until condition is met with `while`
64+
65+
A simplified version of this loop can omit the initialization and increment step. You are then left with the condition step only. This step tests whether a variable is true or false and the loop exits on false. Here's an example:
66+
67+
```go
68+
i := 1
69+
for i < 10 {
70+
i++
71+
// do something
72+
}
73+
```
74+
75+
In this case, we are declaring `i` outside of the loop. Within the loop, we need to change the value to something that will make the loop expression evaluate to false or it will loop forever.
76+
77+
Here's another code, using the same idea, but this time we ask for input from the user:
78+
79+
```go
80+
var keepGoing = true
81+
answer := ""
82+
for keepGoing {
83+
fmt.Println("Type command: ")
84+
fmt.Scan(&answer)
85+
if answer == "quit" {
86+
keepGoing = false
87+
}
88+
}
89+
fmt.Println("program exit")
90+
```
91+
92+
An example run of the program could look like so:
93+
94+
```bash
95+
Type command: test
96+
Type command: something
97+
Type command: quit
98+
program exit
99+
```
100+
101+
## Using `for each` over a range
102+
103+
For this next loop construct, the idea is to operate over an array or some kind of known sequence. For each iteration you are able to get the index as as well as the next item in the loop. Here's some example code:
104+
105+
```go
106+
arr := []string{"arg1", "arg2", "arg3"}
107+
for i, s := range arr {
108+
fmt.Printf("index: %d, item: %s \n", i, s)
109+
}
110+
```
111+
112+
`arr` is defined as an array and then the `range` construct is used to loop over the array. For each iteration, the current index is being assigned to `i` and the array item is assigned to `s`. An output of the above code will look like so:
113+
114+
```output
115+
index: 0, item: arg1
116+
index: 1, item: arg2
117+
index: 2, item: arg3
118+
```
119+
120+
## Controlling the loop with `continue` and `break`
121+
122+
So far, you've seen three ways you can use the `for` construct. There are also ways to control the loop. You can control the loop with the following keywords:
123+
124+
- `break`, this will exit the loop construct
125+
126+
```go
127+
arr = []int{-1,2}
128+
for i := 0; i< 2; i++ {
129+
fmt.Println(arr[i])
130+
if arr[i] < 0 {
131+
break;
132+
}
133+
}
134+
```
135+
136+
The output will be:
137+
138+
```output
139+
-1
140+
```
141+
142+
it won't output `2` as the loop exits after the first iteration.
143+
144+
- `continue`, this will move on to the next iteration. If `break` exits the loop, `continue` skips the current iteration:
145+
146+
```go
147+
arr = []int{-1,2,-1, 3}
148+
for i := 0; i< 4; i++ {
149+
if arr[i] < 0 {
150+
break;
151+
}
152+
fmt.Println(arr[i])
153+
}
154+
```
155+
156+
The output will be:
157+
158+
```output
159+
2
160+
3
161+
```
162+
163+
## Summary
File renamed without changes.

01-basics/06-user-input/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Reading user input
2+
3+
It's an important thing to be able to read user input from the console. How to do that is by using the built-in `fmt`.
4+
5+
The `fmt` library has a built-in `Scan()` method.
6+
7+
## Reading one input
8+
9+
You might start out wanting to read one input from the user. That's what the `Scan()` method does by default.
10+
11+
Here's some code showing how to collect user input:
12+
13+
```go
14+
package main
15+
16+
import "fmt"
17+
18+
func main() {
19+
var response string
20+
fmt.Scan(&response)
21+
fmt.Println("User typed: ", response)
22+
}
23+
```
24+
25+
Not how you send in the string `response` as a reference, using the `&` operator. It's done this way as the `Scan()` method will modify the variable you send in.
26+
27+
When you run this code, you will see the below output:
28+
29+
```output
30+
hello # this is what we typed
31+
User typed: hello
32+
```
33+
34+
## Reading multiple inputs
35+
36+
You can provide several arguments to the `Scan()` method. By providing several arguments, you can collect more than one user input and separate each input with a comma. Here's how to apply this technique:
37+
38+
```go
39+
var a1, a2 string
40+
// multiple input
41+
fmt.Scan(&a1, &a2)
42+
43+
// formatted string
44+
str := fmt.Sprintf("a1: %s a2: %s", a1, a2)
45+
```
46+
47+
Note how `a1` and `a2` is sent into `Scan()` and they are comma separated, so how will those code run?
48+
49+
When you run this code, there's two ways for the user to input. The user can either separate the values by space like so:
50+
51+
```output
52+
hi you
53+
a1: hi a2: you
54+
```
55+
56+
or by newline:
57+
58+
```output
59+
hi
60+
you
61+
a1: hi a2: you
62+
```
63+
64+
## Scanf, scan the input with formatters
65+
66+
So far, you've seen how you can collect input with spaces and endlines as separators. You can also be very specific in how you collect input. Imagine that the user wants to type in an invoice number like so "INV200" or "INV114" and what you are interested in is the number part, or maybe you are interested in the prefix as well? The answer to solving this is in the `Scanf()` function. It takes formatters. With formatters, you're able to pick the part of the user input you are interested in, and place that into the variable you want.
67+
68+
In the above mentioned case, you can construct code looking like so:
69+
70+
```go
71+
var prefix string
72+
var no int
73+
// in110
74+
fmt.Scanf("%3s%d", &prefix, &no)
75+
fmt.Printf("prefix: %s, invoice no: %d", prefix, no)
76+
```
77+
78+
The interesting part lies in the first argument of `Scanf()`, namely `53s%d`. What the means is, take the first 3 characters, `%3s` and interpret as a string. Then interpret and place the remaining as a number, `%d`.
79+
80+
Running this program you will get an output like so:
81+
82+
```output
83+
inv200
84+
prefix: inv, invoice no: 200
85+
```
86+
87+
"inv" is placed in `prefix` and 200 in `no` variable.
88+
89+
## Learn more
90+
91+
To learn more about this are, check out this link <https://pkg.go.dev/fmt#Scanf>
File renamed without changes.

01-basics/06-user-input/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// var response string
7+
fmt.Println("hi")
8+
9+
// // one input
10+
// fmt.Scan(&response)
11+
// fmt.Println("User typed ", response)
12+
13+
// var a1, a2 string
14+
15+
// // multiple input
16+
// fmt.Scan(&a1, &a2)
17+
18+
// // formatted string
19+
// str := fmt.Sprintf("a1: %s a2: %s", a1, a2)
20+
// fmt.Println(str)
21+
22+
var prefix string
23+
var no int
24+
// in110
25+
fmt.Scanf("%3s%d", &prefix, &no)
26+
fmt.Printf("prefix: %s, invoice no: %d", prefix, no)
27+
28+
}

01 basics/06 - functions/README.md renamed to 01-basics/07-functions/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Functions
22

3-
So far, you've seen the function `main`, define like so:
3+
So far, you've seen the function `main()`, define like so:
44

55
```go
66
func main(){
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)