Home > Backend Development > Golang > Slice returns unexpected length

Slice returns unexpected length

WBOY
Release: 2024-02-09 10:21:29
forward
698 people have browsed it

Slice returns unexpected length

php editor Youzi often encounters the problem of returning unexpected lengths when using slicing operations. Slicing operation is one of the commonly used array operations. You can obtain some elements of the array by specifying the starting position and ending position. However, sometimes we find that the returned slice length is not as expected, which may be due to some common mistakes or misunderstandings. In this article, we will introduce in detail the problems you may encounter during slicing operations and provide solutions to help you better understand and use slicing operations.

Question content

I was working on golang and I stopped and it confused me.

package main

import "fmt"

func main() {
    month := [...]string{1: "jan", 2: "fab", 3: "march", 4: "april", 5: "may", 6: "june", 7: "july", 8: "aug", 9: "sep", 10: "oct", 11: "nov", 12: "dec"}
    fmt.println(cap(month))
    summer := month[6:9]
    q2 := month[4:7]
    fmt.println(cap(q2))
    fmt.println(len(q2))

    fmt.println(cap(summer))
    fmt.println(len(summer))
}
Copy after login

The output is

13
9
3
7
3
Copy after login

The month slice has 12 elements, but cap(month) and len(month) return 13, why?

Solution

First, month is an array - not a slice - whose type is [13]string. Just looking at its type, we know that it has 13 elements (length) and the slice is of type []string.

Array and slice indexes start from zero, not from one. Since you did not specify a string value for the array at index 0:

month := [...]string{1: "jan", 2: "fab", 3: "march", 4: "april", 5: "may", 6: "june", 7: "july", 8: "aug", 9: "sep", 10: "oct", 11: "nov", 12: "dec"}
Copy after login

It is equivalent to:

month := [13]string{0: "", 1: "jan", 2: "fab", 3: "march", 4: "april", 5: "may", 6: "june", 7: "july", 8: "aug", 9: "sep", 10: "oct", 11: "nov", 12: "dec"}
Copy after login

That is, the zero value (i.e. the empty string) of string is provided as the first element.

Please note that I have replaced ... with 13. The ellipsis tells the compiler to infer the length of the array (which is part of its type) based on the initializer.

Even if you use a slice literal instead of an array literal as an initializer:

month := []string{1: "Jan", 2: "Fab", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "Aug", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec"}
Copy after login

For the same reasons as above, the length of (in this case the slice) month will still be 13.

The above is the detailed content of Slice returns unexpected length. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template