add: slice internals backing array and slice header
This commit is contained in:
@@ -1,44 +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() {
|
||||
// ages, first and last2 have the same backing arrays
|
||||
ages := []int{35, 15, 25}
|
||||
first := ages[0:1]
|
||||
last2 := ages[1:3]
|
||||
|
||||
ages[0] = 55
|
||||
ages[1] = 10
|
||||
ages[2] = 20
|
||||
|
||||
// grades and ages have separate backing arrays
|
||||
grades := []int{70, 99}
|
||||
grades[0] = 50
|
||||
|
||||
s.Show("ages", ages)
|
||||
s.Show("ages[0:1]", first)
|
||||
s.Show("ages[1:3]", last2)
|
||||
s.Show("grades", grades)
|
||||
|
||||
// let's create a new scope
|
||||
// 'cause i'm going to use variables with the same name
|
||||
{
|
||||
// ages and agesArray have the same backing arrays
|
||||
agesArray := [3]int{35, 15, 25}
|
||||
ages := agesArray[0:3]
|
||||
|
||||
ages[0] = 100
|
||||
ages[2] = 50
|
||||
|
||||
s.Show("agesArray", agesArray[:])
|
||||
s.Show("agesArray's ages", ages)
|
||||
}
|
||||
}
|
@@ -1,50 +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() {
|
||||
// #1: arrays and non-empty slice literals create an array.
|
||||
// For the arrays, it's explicit, but for the slices,
|
||||
// it's done implicitly, behind the scenes.
|
||||
|
||||
grades := [...]float64{40, 10, 20, 50, 60, 70} // #1
|
||||
// grades := []float64{40, 10, 20, 50, 60, 70} // #4
|
||||
|
||||
// #5: let's break the connection
|
||||
// #6: comment-out
|
||||
// var newGrades []float64
|
||||
// newGrades = append(newGrades, grades...)
|
||||
|
||||
// #6: shortcut: []float64(nil) is a nil float64 slice
|
||||
// newGrades := append([]float64(nil), grades...)
|
||||
|
||||
// #2: cheap: slicing doesn't allocate new memory (array).
|
||||
// front := grades[:3]
|
||||
|
||||
// front := newGrades[:3] // #5
|
||||
|
||||
// #3: sort its first segment
|
||||
// sort.Float64s(front)
|
||||
|
||||
// #7: new slices look at the same backing array
|
||||
// front, front2, front3, newGrades, they all have the same backing array
|
||||
// front2 := front[:3]
|
||||
// front3 := front
|
||||
|
||||
s.PrintBacking = true // #1
|
||||
s.MaxPerLine = 7 // #1
|
||||
s.Show("grades", grades[:]) // #1
|
||||
// s.Show("newGrades", newGrades) // #5
|
||||
// s.Show("front", front) // #2
|
||||
// s.Show("front2", front2) // #7
|
||||
// s.Show("front3", front3) // #7
|
||||
}
|
@@ -1,21 +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() {
|
||||
ages := []int{35, 15, 25}
|
||||
first, last := ages[0:1], ages[1:3]
|
||||
|
||||
s.Show("ages", ages)
|
||||
s.Show("first", first)
|
||||
s.Show("last", last)
|
||||
|
||||
s.Show("nil slice", []int(nil))
|
||||
}
|
@@ -1,61 +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"
|
||||
"unsafe"
|
||||
|
||||
s "github.com/inancgumus/prettyslice"
|
||||
)
|
||||
|
||||
// type collection [4]string // #1
|
||||
type collection []string // #2
|
||||
|
||||
// go is pass by copy
|
||||
// only the slice header is copied: 3 integer fields (24 bytes)
|
||||
// think of passing an array with millions of elements.
|
||||
|
||||
func main() {
|
||||
// SliceHeader lives here:
|
||||
// https://golang.org/src/runtime/slice.go
|
||||
|
||||
s.PrintElementAddr = true
|
||||
|
||||
// #1
|
||||
data := collection{"slices", "are", "awesome", "period", "!!" /* #5 */}
|
||||
|
||||
// data := collection{"slices", "are", "awesome", "period", "!!"}
|
||||
|
||||
change(data) // #1
|
||||
|
||||
s.Show("main's data", data) // #1
|
||||
fmt.Printf("main's data slice's header: %p\n", &data) // #3
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// #4
|
||||
array := [...]string{"slices", "are", "awesome", "period", "!!" /* #5 */}
|
||||
|
||||
// array's size depends on its elements
|
||||
fmt.Printf("array's size: %d bytes.\n", unsafe.Sizeof(array))
|
||||
|
||||
// slice's size is always fixed: 24 bytes (on a 64-bit system) — slice value = slice header
|
||||
fmt.Printf("slice's size: %d bytes.\n", unsafe.Sizeof(data))
|
||||
}
|
||||
|
||||
// #1
|
||||
// passed value will be copied in the function
|
||||
func change(data collection) {
|
||||
// data is a new variable inside the function:
|
||||
// var data collection
|
||||
|
||||
data[2] = "brilliant!"
|
||||
|
||||
s.Show("change's data", data)
|
||||
fmt.Printf("change's data slice's header: %p\n", &data) // #3
|
||||
}
|
Reference in New Issue
Block a user