Skip to content

Commit 4d302a1

Browse files
author
Rajeev Kumar Singh
committed
Restructured code into directories
1 parent 5356eb0 commit 4d302a1

File tree

49 files changed

+102
-89
lines changed

Some content is hidden

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

49 files changed

+102
-89
lines changed
File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [5]string{"Alpha", "Beta", "Gamma", "Delta", "Epsilon"}
7+
8+
// Creating a slice from the array
9+
var s []string = a[1:4]
10+
11+
fmt.Println("Array a = ", a)
12+
fmt.Println("Slice s = ", s)
13+
14+
/*
15+
low and high parameters are optional in a[low:high]
16+
The default value for low is 0, and high is the length of the slice.
17+
*/
18+
slice1 := a[1:4]
19+
slice2 := a[:3]
20+
slice3 := a[2:]
21+
slice4 := a[:]
22+
23+
fmt.Println("slice1 = ", slice1)
24+
fmt.Println("slice2 = ", slice2)
25+
fmt.Println("slice3 = ", slice3)
26+
fmt.Println("slice4 = ", slice4)
27+
}
File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
/*
7+
The length of the slice is the number of elements in the slice.
8+
The capacity is the number of elements in the underlying array starting from the first element in the slice.
9+
*/
10+
a := [6]int{10, 20, 30, 40, 50, 60}
11+
s := a[1:4]
12+
13+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Creates an array of size 10, slices it till index 5, and returns the slice reference
7+
s := make([]int, 5, 10)
8+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
9+
10+
/*
11+
The capacity parameter in the make() function is optional. When omitted, it defaults to the specified length
12+
*/
13+
// Creates an array of size 5, and returns a slice reference to it
14+
var s1 = make([]int, 5)
15+
fmt.Printf("s1 = %v, len = %d, cap = %d\n", s1, len(s1), cap(s1))
16+
17+
}

07-slices/slice_zero_value.go renamed to 07-slices/08-slice-zero-value/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import "fmt"
44

55
func main() {
6+
// The zero value of a slice is nil. A nil slice doesn’t have any underlying array, and has a length and capacity of 0
67
var s []int
78
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
89

07-slices/slice_copy.go renamed to 07-slices/09-slice-copy/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package main
33
import "fmt"
44

55
func main() {
6+
/*
7+
The copy() function copies elements from one slice to another
8+
func copy(dst, src []T) int
9+
*/
10+
611
src := []string{"Sublime", "VSCode", "IntelliJ", "Eclipse"}
712
dest := make([]string, 2)
813

07-slices/10-slice-append/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// The append() function appends new elements at the end of a given slice.
7+
8+
// Appending to a slice that doesn't have enough capacity to accommodate new elements
9+
slice1 := []string{"C", "C++", "Java"}
10+
slice2 := append(slice1, "Python", "Ruby", "Go")
11+
12+
fmt.Printf("slice1 = %v, len = %d, cap = %d\n", slice1, len(slice1), cap(slice1))
13+
fmt.Printf("slice2 = %v, len = %d, cap = %d\n", slice2, len(slice2), cap(slice2))
14+
15+
slice1[0] = "C#"
16+
fmt.Println("\nslice1 = ", slice1)
17+
fmt.Println("slice2 = ", slice2)
18+
19+
/*
20+
In the above example, since slice1 has capacity 3, it can’t accommodate more elements.
21+
So a new underlying array is allocated with bigger capacity when we append more elements to it.
22+
So if you modify slice1, slice2 won’t see those changes because it refers to a different array.
23+
*/
24+
}

07-slices/slice_append_capacity.go renamed to 07-slices/11-slice-append-capacity/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import "fmt"
44

55
func main() {
6+
// Appending to a slice that has enough capacity to accommodate new elements
67
slice1 := make([]string, 3, 10)
78
copy(slice1, []string{"C", "C++", "Java"})
89

@@ -14,4 +15,8 @@ func main() {
1415
slice1[0] = "C#"
1516
fmt.Println("\nslice1 = ", slice1)
1617
fmt.Println("slice2 = ", slice2)
18+
19+
/*
20+
In this case, no new array would be allocated, and the elements would be added to the same underlying array.
21+
*/
1722
}
File renamed without changes.
File renamed without changes.

07-slices/slice_append.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

07-slices/slice_defaults.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

07-slices/slice_from_array.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

07-slices/slice_length_capacity.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

07-slices/slice_make.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

07-slices/slice_make_default_capacity.go

Lines changed: 0 additions & 9 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

08-maps/map_delete_key.go renamed to 08-maps/07-map-delete-key/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import "fmt"
44

55
func main() {
6+
// You can delete a key from a map using the built-in delete() function
67
var fileExtensions = map[string]string{
78
"Python": ".py",
89
"C++": ".cpp",

08-maps/map_reference_type.go renamed to 08-maps/08-map-reference-type/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package main
33
import "fmt"
44

55
func main() {
6+
/*
7+
Maps are reference types. When you assign a map to another variable, they both refer to the same underlying data structure.
8+
Therefore changes done by one variable will be visible to the other
9+
*/
610
var m1 = map[string]int{
711
"one": 1,
812
"two": 2,
File renamed without changes.

09-pointers/pointer_dereferencing_modify.go renamed to 09-pointers/03-pointer-dereference/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package main
33
import "fmt"
44

55
func main() {
6-
var a = 1000
6+
var a = 100
77
var p = &a
88

9-
fmt.Println("a (before) = ", a)
9+
fmt.Println("a = ", a)
10+
fmt.Println("p = ", p)
11+
fmt.Println("*p = ", *p)
1012

1113
// Changing the value stored in the pointed variable through the pointer
1214
*p = 2000
13-
1415
fmt.Println("a (after) = ", a)
1516
}

09-pointers/pointer_dereferencing.go

Lines changed: 0 additions & 12 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)