This is a creation in Article, where the information may have evolved or changed.
package mainimport ("FMT") Func main () {arr := [...] string{"Go", "python", "java", "C + +", "C"}fmt. Println ("arr value ", arr) fmt. Println ("------------------------") slice1 := arr[1:3]fmt. Println ("slice1 now is ", slice1) fmt. Println ("len of slice1 is ", len (Slice1), ", Cap of sclie1 is ", cap (Slice1)) fmt. Println ("------------------------") slice2 := append (slice1, "Ruby", "Obj-c") // If you add another PHP, it will be a completely different picture ... Fmt. Println ("slice2 is ", slice2) fmt. Println ("len of slice2 is ", len (Slice2), ", Cap of sclie2 is ", cap (Slice2)) fmt. Println ("after append : slice1 is ", slice1) //The underlying array, though changed, But Slice1 Len did not change the FMT. Println ("len of slice1 is ", len (Slice1), ", Cap of sclie1 is ", &nbsP;cap (Slice1)) fmt. Println ("after append : arr value is ", arr) fmt. Println ("------------------------") Slice1 = slice1[:cap (Slice1)] //this time is right ... Fmt. Println ("after reslice : slice1 is ", slice1) fmt. Println ("len of slice1 is ", len (Slice1), ", Cap of sclie1 is ", cap (Slice1))}
Output Result:
arr value [go python java  C++ C]------------------------slice1 now is [python java]len of slice1 is 2 ,cap of sclie1 is 4------------------------ Slice2 is [python java ruby obj-c]len of slice2 is 4 ,cap of sclie2 is 4after append : slice1 is [ python java]len of slice1 is 2 ,cap of sclie1 is 4after append : arr value is [go python java ruby  OBJ-C]------------------------After reslice : slice1 is [python java ruby obj-c]len of slice1 is 4 ,cap of sclie1 is 4