Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.
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+
}
File renamed without changes.
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /