Problem source: as shown in the figure below, I declare a slice that prompts me that the subscript is out of bounds
let’s debug why this happens
we can see that the slice in this declaration mode stores not zero values, but nil
in fact, we just need to change it to var SS = make ([] int, n)
next, let’s look at the principle and deeply analyze the three special state zero slices of go language slice, Empty slice and nil slice
the bottom layer of slice is an array, and the surface layer of slice is a structure containing three variables. When we assign a slice to another slice, it is essentially a shallow copy of the surface structure of slice. The first variable in the structure is a pointer to the underlying array, and the other two variables are the length and capacity of the slice
let’s first look at the simplest zero slice
then empty slice and nil slice
the above four forms seem to be the same from the output results. But in fact, there are differences. The two special types of “empty slice” and “nil slice” we want to talk about are hidden in the above four forms. How can we analyze the differences between the internal structures of three sides and four forms?Next, we will use the high-level content of go language to convert any variable type of go language through unsafe. Pointer. Because the internal structure of the slice is a structure body, which contains three machine word size integer variables. The first variable is a pointer variable, and the pointer variable also stores an integer value, but this value is the memory address of another variable. We can look at this structure as an integer array [3] int with a growth of 3. Then convert the slice variable to [3] int
from the output, we see obvious, magical, surprising and incomprehensible different results. The S1 and S4 variables whose output is [0] are “nil slices”, and the S2 and S3 variables are “empty slices”. 824634199592 this value is a special memory address shared by all types of “empty slices”
the last question is: is there any difference between “nil slice” and “empty slice”?The official recommends nil slices. See why
“empty slice” and “nil slice” are sometimes hidden in the structure. At this time, their differences are ignored by too many people. Let’s take an example
it can be found that the results of creating structures are different between the two types! ” Another great difference between “empty slice” and “nil slice” is JSON serialization, which is extremely important
Read More:
- [Solved] electron-builder Package mac Error: panic: runtime error: index out of range
- Android Studio Error: String index out of range 0 [How to Solve]
- runtime error ‘9’: subscript out of range error in VBA programming
- [Go] Solve panic: runtime error: invalid memory address or nil pointer dereference in golang
- [Solved] panic: runtime error: invalid memory address or nil pointer dereference
- C++ Error: terminating with uncaught exception of type std::out_of_range: vector Abort trap: 6
- [Solved] Docker Startup Error: panic: runtime error: invalid memory address or nil pointer dereference
- [Solved] Tsingsee green rhinoceros video pedestrian intelligence detection test error panic: runtime error
- [Solved] MindSpore Error: ValueError: `padding_idx` in `Embedding` out of range
- How to Solve “RIP Address Out Of Range” Error
- [Solved] Android DataBinding Error: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment Changes to Fragm
- [Solved] TensorFlow severing Container Creat Error: failed: Out of range: Read less bytes than requested
- [Solved] vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: “TypeError: Cannot read property ‘length‘
- [Solved] docker Error response from daemon OCI runtime create failed container_linux.go380
- [Solved] Error running ‘go build myfir_go_project‘: Cannot run program
- [Solved] Golang Error: main.go:5:2: package go_code/chapter10/factory/model is not in GOROOT
- Go declares that the local variable does not use command line arguments. Main. Go: 4:6: a declared but not used
- openlayers — Cannot read property ‘slice‘ of null—Map cannot be loaded
- [Solved] .\main.go:3:6: missing function body .\main.go:4:1: syntax error: unexpected semicolon or newline be
- [Solved] Error: The slice reducer for key “auth“ returned undefined during initialization. If the state pas