add: slice internals last lectures
This commit is contained in:
@ -1,31 +0,0 @@
|
||||
// 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"
|
||||
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s.MaxPerLine = 6
|
||||
s.PrintBacking = true
|
||||
|
||||
ages := []int{35, 15, 25}
|
||||
s.Show("ages", ages)
|
||||
|
||||
s.Show("ages[0:0]", ages[0:0])
|
||||
|
||||
for i := 1; i < 4; i++ {
|
||||
txt := fmt.Sprintf("ages[%d:%d]", 0, i)
|
||||
s.Show(txt, ages[0:i])
|
||||
}
|
||||
|
||||
s.Show("append", append(ages, 50))
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
// 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 nil slice
|
||||
var games []string // nil slice
|
||||
s.Show("games", games)
|
||||
|
||||
// ----------------------------------------------------
|
||||
// #2 empty slice
|
||||
games = []string{} // empty slice
|
||||
s.Show("games", games)
|
||||
// s.Show("another empty", []int{})
|
||||
|
||||
// ----------------------------------------------------
|
||||
// #3 non-empty slice
|
||||
games = []string{"pacman", "mario", "tetris", "doom"}
|
||||
s.Show("games", games)
|
||||
|
||||
// ----------------------------------------------------
|
||||
// #4 reset the part using the games slice
|
||||
// part is empty but its cap is still 4
|
||||
part := games
|
||||
s.Show("part", part)
|
||||
|
||||
part = games[:0]
|
||||
s.Show("part[:0]", part)
|
||||
s.Show("part[:cap]", part[:cap(part)])
|
||||
|
||||
for cap(part) != 0 {
|
||||
part = part[1:cap(part)]
|
||||
s.Show("part", part)
|
||||
}
|
||||
|
||||
// #6 backing array's elements become inaccessible
|
||||
// games = games[len(games):]
|
||||
|
||||
// ----------------------------------------------------
|
||||
// #5 part doesn't have any more capacity
|
||||
// games slice is still intact
|
||||
s.Show("part", part)
|
||||
s.Show("games", games)
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
// 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
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
// 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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user