add: slices - first part

This commit is contained in:
Inanc Gumus
2018-12-18 15:20:37 +03:00
parent 3ac61333fd
commit d2be3d6692
92 changed files with 2284 additions and 683 deletions

View File

@@ -0,0 +1,30 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
{
// its length is part of its type
var nums [5]int
fmt.Printf("nums array: %#v\n", nums)
}
{
// its length is not part of its type
var nums []int
fmt.Printf("nums slice: %#v\n", nums)
fmt.Printf("len(nums) : %d\n", len(nums))
// won't work: the slice is nil.
// fmt.Printf("nums[0]: %d\n", nums[0])
// fmt.Printf("nums[1]: %d\n", nums[1])
}
}

View File

@@ -0,0 +1,38 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
var array [2]int
// zero value of an array is zero-valued elements
fmt.Printf("array : %#v\n", array)
// nope: arrays are fixed length
// array[2] = 0
var slice []int
// zero value of a slice is nil
fmt.Println("slice == nil?", slice == nil)
// nope: they don't exist:
// _ = slice[0]
// _ = slice[1]
// len function still works though
fmt.Println("len(slice) :", len(slice))
// array's length is part of its type
fmt.Printf("array's type: %T\n", array)
// whereas, slice's length isn't part of its type
fmt.Printf("slice's type: %T\n", slice)
}

View File

@@ -0,0 +1,52 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
// STORY:
// You want to list the books and games you have
func main() {
var books [5]string
books[0] = "dracula"
books[1] = "1984"
books[2] = "island"
newBooks := [5]string{"ulysses", "fire"}
if books == newBooks {
}
books = newBooks
games := []string{"kokemon", "sims"}
newGames := []string{"pacman", "doom", "pong"}
newGames = games
games = nil
games = []string{}
var ok string
for i, game := range games {
if game != newGames[i] {
ok = "not "
break
}
}
if len(games) != len(newGames) {
ok = "not "
}
fmt.Printf("games and newGames are %sequal\n\n", ok)
fmt.Printf("books : %#v\n", books)
fmt.Printf("new books : %#v\n", newBooks)
fmt.Printf("games : %T\n", games)
fmt.Printf("new games : %#v\n", newGames)
fmt.Printf("games's length: %d\n", len(games))
fmt.Printf("games's nil : %t\n", games == nil)
}

View File

@@ -0,0 +1,42 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
const max = 5
var uniques [max]int
// It's harder to make a program dynamic using arrays
// max, _ := strconv.Atoi(os.Args[1])
// var uniques [10]int
loop:
for found := 0; found < max; {
n := rand.Intn(max) + 1
fmt.Print(n, " ")
for _, u := range uniques {
if u == n {
continue loop
}
}
uniques[found] = n
found++
}
fmt.Println("\n\nuniques:", uniques)
}

View File

@@ -0,0 +1,56 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"math/rand"
"os"
"sort"
"strconv"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
max, _ := strconv.Atoi(os.Args[1])
// declare an uninitialized nil slice
var uniques []int
loop:
// you can still use the len function on a nil slice
for len(uniques) < max {
n := rand.Intn(max) + 1
fmt.Print(n, " ")
for _, u := range uniques {
if u == n {
continue loop
}
}
// append can add new elements the slice
uniques = append(uniques, n)
// a slice doesn't contain any elements from the beginning
// uniques[found] = n
// found++
}
fmt.Println("\n\nuniques:", uniques)
fmt.Println("\nlength of uniques:", len(uniques))
sort.Ints(uniques)
fmt.Println("\nsorted:", uniques)
nums := [5]int{5, 4, 3, 2, 1}
sort.Ints(nums[:])
fmt.Println("\nnums:", nums)
}

8
16-slices/README.md Normal file
View File

@@ -0,0 +1,8 @@
# WARNING
For the code in this section, you should install my prettyslice library.
## STEPS
1. Open your command-line
2. Type: `go get -u github.com/inancgumus/prettyslice`
3. That's all.

View File

@@ -0,0 +1,122 @@
# Slices vs Arrays Quiz
## Why you want to use a slice instead of an array?
1. I like arrays more
2. I want to create a dynamic collection, so I need an array
3. A slice's length is dynamic, so I can create dynamic collections *CORRECT*
## When does the length of a slice is determined?
1. At compile-time
2. In runtime *CORRECT*
3. When a program terminates
> **2:** A slice's length is not a part of its type. So its length can change at runtime.
## Which function call below is correct?
```go
// Let's say there's a function like this.
func sort(nums []int) {
// ...
}
```
1. sort([...]int{3, 1, 6})
2. sort([]int32{3, 1, 6})
3. sort([]int{3, 1, 6}) *CORRECT*
> **1:** You can't call the sort function using an array. It expects an int slice.
>
> **2:** You can't call the sort function using an int32 slice. It expects an int slice.
>
> **3:** That's right! You can pass an int slice to the sort function.
## What is the zero value of this slice?
```go
var tasks []string
```
1. 0
2. 1
3. nil *CORRECT*
4. unknown
> **3:** This is a nil slice. Unlike an array, a slice's zero value is nil.
## What does this code print?
```go
var tasks []string
fmt.Println(len(tasks))
```
1. 0 *CORRECT*
2. 1
3. nil
4. It doesn't work.
> **1:** Yes, you can use the len function on a nil slice. It returns 0 because the slice doesn't contain any elements yet.
## What does this code print?
```go
var tasks []string
fmt.Println(tasks[0])
```
1. 0
2. 1
3. nil
4. It doesn't work. *CORRECT*
> **4:** You can't get an element that does not exist. A nil slice does not contain any elements.
## Which declaration below is a correct slice declaration?
1. [...]int{}
2. [2]string{"hello", "world"}
3. []string{"hello", "world"} *CORRECT*
4. string[2]{"hello", world"}
## This code doesn't work, why?
```go
colors := []string{"red", "blue", "green"}
tones := []string{"dark", "light"}
if colors == tones {
// ...
}
```
1. The slices have different lengths
2. If statement doesn't contain any statements
3. Slices cannot be compared *CORRECT*
> **3:** That's right! A slice value can only be compared to a nil value.
## What is the length of this slice?
```go
[]uint64{}
```
1. 64
2. 1
3. 0 *CORRECT*
4. Error
> **3:** That's right. This is an empty slice, it doesn't contain any elements.
## What is the length of this slice?
```go
[]string{"i'm", "going", "to", "stay", "\"here\""}
```
1. 0
2. 1
3. 2
4. 3
5. 4
6. 5 *CORRECT*

View File

@@ -0,0 +1,3 @@
# Slice Quizzes
* [Slices vs Arrays](1-slices-vs-arrays.md)