Skip to content

Commit d0d30c0

Browse files
shrn01ronreiter
authored andcommitted
Added Slices.md and added for-range loop in Loops
1 parent 3f5e5e8 commit d0d30c0

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

tutorials/learn-golang.org/en/Loops.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ You can omit the init and post statement in the `for` loop to get a while loop.
4646
}
4747
}
4848

49+
Golang also provides a for-range loop to loop over an array, slice or few other datastructures.
50+
51+
The for-range loop provides us access to the index and value of the elements in the array or slice we are looping through as shown below
52+
53+
package main
54+
55+
import "fmt"
56+
57+
func main() {
58+
myList := []int{1,2,3}
59+
60+
for index, value in range myList {
61+
fmt.Printf("%d is index, %d is value", index, value)
62+
}
63+
}
64+
65+
We can ignore one or both the fields in a for-range loop by giving an `_` instead of giving a variable name
66+
67+
for _, value in range myList {
68+
fmt.Println(value)
69+
}
4970

5071
Exercise
5172
--------
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Tutorial
2+
--------
3+
4+
We already studied about the arrays in golang. But there is one drawback when using golang, they are of fixed size. If we want to increase the size, we need to create another array with higher capacity and copy over the values from the old array. Slices solve this problem.
5+
6+
## Slices
7+
8+
Slices are like arrays but they are dynamically resizable i.e. their size is not fixed similar to `list` in python. Slices are a layer of abstraction over arrays. So when on resizing, the underlying array is changed to a larger array and the contents are copied, but it is completely abstracted from the programmer.
9+
10+
The syntax for defining a slice is
11+
12+
var <slice name> []<datatype of data stored in the slice>
13+
14+
This is an example of defining a slice.
15+
16+
var exampleSlice []int
17+
18+
To add elements to a slice we use the `append()` function, as shown below
19+
20+
exampleSlice = append(exampleSlice, 1)
21+
exampleSlice = append(exampleSlice, 2)
22+
exampleSlice = append(exampleSlice, 3)
23+
24+
We can do this in a single line like this
25+
26+
exampleSlice := []int{1,2,3}
27+
28+
Remember we studied about the `:=` operator in go in the previous chapters. It enables us to skip adding the var keyword and the type.
29+
30+
To find the current length of the slice we use the `len()` function, and to find the capacity of the underlying array, we use the `cap()` function
31+
32+
fmt.Printf("%d is the length of the Slice", len(exampleSlice)) // prints 3
33+
fmt.Printf("%d is the capacity of the underlying array", cap(exampleSlice))
34+
35+
Now let's print the elements of the slice
36+
37+
fmt.Println(exampleSlice) // prints [1 2 3]
38+
39+
You can also create a slice using the make function where you can also give an initial capacity to the slice.
40+
41+
// here 0 is the initial length, and 5 is the initial capacity
42+
// i.e. we haven't added any values but the underlying array has a
43+
// initial capacity of 5 which is later changed dynamically to meet the requirement
44+
exampleSlice := make([]int, 0, 5)
45+
46+
// You can skip the capacity and length and only give the type to get an empty slice
47+
exampleSlice = make([]int)
48+
49+
Exercise
50+
--------
51+
Create a string slice using make and add elements `"Hello", "World", "!"` to the slice using the append function, or using the one line declaration format and print the Slice.
52+
53+
Tutorial Code
54+
-------------
55+
package main
56+
57+
import "fmt"
58+
59+
func main () {
60+
// Add your code here.
61+
}
62+
63+
Expected Output
64+
---------------
65+
[Hello World !]
66+
67+
Solution
68+
--------
69+
package main
70+
71+
import "fmt"
72+
73+
func main () {
74+
exampleSlice := []string{"Hello", "World", "!"}
75+
fmt.Println(exampleSlice)
76+
}

tutorials/learn-golang.org/en/Welcome.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ learn-golang.org is still under construction - If you wish to contribute tutoria
1313
- [[Hello, World!]]
1414
- [[Variables]]
1515
- [[Arrays]]
16+
- [[Slices]]
1617
- [[If-Else]]
1718
- [[Loops]]
1819

0 commit comments

Comments
 (0)