Last active
November 13, 2021 17:59
-
-
Save ewrvp7lv7/44500638c62ca0fd23e0fa39658b67af to your computer and use it in GitHub Desktop.
Slice external and internal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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