Skip to content

Instantly share code, notes, and snippets.

@ewrvp7lv7
Last active November 13, 2021 17:59
Show Gist options
  • Save ewrvp7lv7/44500638c62ca0fd23e0fa39658b67af to your computer and use it in GitHub Desktop.
Save ewrvp7lv7/44500638c62ca0fd23e0fa39658b67af to your computer and use it in GitHub Desktop.
Slice external and internal
package main
import "fmt"
//Pass slice through the function
//function have own slise wich have common part with external slice
//but! if inner slice change own size, external slise isn't changed
func main() {
arr := make([]byte, 16)
fmt.Println("hello01", len(arr), arr) //hello01 16 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
cutArrSize(arr)
fmt.Println("hello02", len(arr), arr) //hello02 16 [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
}
func cutArrSize(arr []byte) {
for len(arr) > 0+8 {
arr[0] = 1 // remembered in external array
arr = arr[1:]
fmt.Println("hello05", len(arr))
}
fmt.Println("hello10", len(arr), arr) //hello10 8 [0 0 0 0 0 0 0 0]
for i := 0; i < 32; i++ {
if len(arr) == i {
arr = append(arr, 0) // not pass in external array
}
// arr[i] = 1
fmt.Println("hello15", len(arr))
}
fmt.Println("hello20", len(arr), arr) //hello20 32 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment