add: slice internals last lectures

This commit is contained in:
Inanc Gumus
2019-02-15 16:30:27 +03:00
parent 17f6b93e46
commit c4e7811078
6 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,22 @@
// 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 (
s "github.com/inancgumus/prettyslice"
)
func main() {
s.PrintBacking = true
ages := []int{35, 15}
s.Show("ages", ages)
ages = append(ages, 5)
s.Show("append(ages, 5)", ages)
}

View File

@ -0,0 +1,54 @@
// 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 (
s "github.com/inancgumus/prettyslice"
)
func main() {
s.PrintBacking = true
// #1: a nil slice has no backing array
var nums []int
s.Show("no backing array", nums)
// #2: creates a new backing array
nums = append(nums, 1, 3)
s.Show("allocates", nums)
// #3: creates a new backing array
nums = append(nums, 2)
s.Show("free capacity", nums)
// #4: uses the same backing array
nums = append(nums, 4)
s.Show("no allocation", nums)
// GOAL: append new odd numbers in the middle
// [1 3 2 4] -> [1 3 7 9 2 4]
// #6: [1 3 2 4] -> [1 3 2 4 2 4]
nums = append(nums, nums[2:]...)
s.Show("nums <- nums[2:]", nums)
// #5: overwrites: [1 3 2 4 2 4] -> [1 3 7 9]
nums = append(nums[:2], 7, 9)
s.Show("nums[:2] <- 7, 9", nums)
// #7: [1 3 7 9] -> [1 3 7 9 2 4]
nums = nums[:6]
s.Show("nums: extend", nums)
}
// don't mind about these options
// they're just for printing the slices nicely
func init() {
s.MaxPerLine = 10
s.Width = 45
}

View File

@ -0,0 +1,34 @@
// 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 (
"math/rand"
"time"
s "github.com/inancgumus/prettyslice"
"github.com/inancgumus/screen"
)
func main() {
s.PrintBacking = true
s.MaxPerLine = 30
s.Width = 150
var nums []int
screen.Clear()
for cap(nums) <= 128 {
screen.MoveTopLeft()
s.Show("nums", nums)
nums = append(nums, rand.Intn(9)+1)
time.Sleep(time.Second / 4)
}
}

View File

@ -0,0 +1,27 @@
// 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() {
ages, oldCap := []int{1}, 1.
for len(ages) < 5e5 {
ages = append(ages, 1)
c := float64(cap(ages))
if c != oldCap {
fmt.Printf("len:%-10d cap:%-10g growth:%.2f\n",
len(ages), c, c/oldCap)
}
oldCap = c
}
}