Two types of slice expressions in the Go language

Source: Internet
Author: User
Prior to this, there have been a lot of slice in Golang, such as * [Go slices:usage and Internals] (https://blog.golang.org/ go-slices-usage-and-internals) * [How to avoid Go Gotchas] (https://blog.golang.org/go-slices-usage-and-internals) This article focuses only on the representations of slice, which can create two types of values: * truncated string* pointer to an array or slice the go language has two representations of slice: a shorthand and a complete expression. # # # abbreviated expression slice the shorthand expression is: ' ' goinput[low:high] ' where low and high are indexes of slice, whose values must be integers, which specify the input operand Which elements can be placed in the slice of the result. The input operand can be a string,array, or a pointer to an array or slice. As a result, the length of slice is high-low. The following example shows: "' gonumbers: = [10]int{0,1,2,3,4,5,6,7,8,9}s: = Numbers[2:4:6]fmt. PRINTLN (s)//[2, 3]fmt. Println (Cap (s))//4 "Applies the slice expression to the pointer to the array, which is the first time to cancel a reference to the pointer and then apply the slice expression in a general manner. A pointer to a slice expression applied to an array is a shorthand for referencing the pointer and then applying the slice expression in the usual way. "' Gonumbers: = [5]int{1, 2, 3, 4, 5}fmt. Println ((&numbers) [1:3])//[2, 3] ' Slice index low and high can be omitted, the default value of low is 0,high the length of the default value of Slice: ' ' gofmt. Println ("foo" [: 2])//"fo" FMT. Println ("foo" [1:])//"oo" FMT. Println ("foo" [:])//"foo", but Slice's index cannot be any of the following types of values: *Low < 0 or High < 0* low <= high* high <= len (input) ' gofmt. Println ("foo" [-1:])//Invalid slice index-1 (index must be non-negative)//fmt. Println ("foo" [: 4])//Invalid slice index 4 (out of the bounds for 3-byte string) fmt. Println ("foo" [2:2])//"" (blank)//fmt. Println ("foo" [2:1])//Invalid slice index:2 > 1 ' "Otherwise, even if an index over the slice range cannot be detected at compile time, a panic will occur at run time. "' Gofunc low () int {return 4}func main () {FMT. Println ("foo" [Low ():])} "" Panic:runtime error:slice bounds out of Rangegoroutine 1 [running]:p anic (0x102280, 0x1040a018)/usr/local/go/src/runtime/panic.go:500 +0x720main.main ()/tmp/sandbox685025974/main.go:12 +0x120 "# # Complete expression This method controls the capacity of the result slice, but only for arrays and pointers to arrays or slice (string is not supported), and the capacity of the result slice in a shorthand expression is the maximum possible capacity starting at index low (a shorthand representation of slice Gonumbers: = [10]int{0,1,2,3,4,5,6,7,8,9}s: = Numbers[1:4]fmt. PRINTLN (s)//[1, 2, 3]fmt. Println (Cap (s))//9 "for an array, cap (a) = = Len (a) in the preceding code fragment, the capacity of S is 9 because the slice starts at index 1, and the underlying array has 8 elements (2 to 9). Complete expression of sliceAllows you to modify this default behavior, as in the following code: "Gonumbers: = [10]int{0,1,2,3,4,5,6,7,8,9}s: = Numbers[1:4:5]fmt. PRINTLN (s)//[1, 2, 3]fmt. Println (Cap (s))//4 "' complete slice expression has the following form: ' ' Goinput[low:high:max ' ' Index low and index high are the same meaning and work as a shorthand expression. The only difference is that Max sets the capacity of the result slice to Max-low. "' Gonumbers: = [10]int{0,1,2,3,4,5,6,7,8,9}s: = Numbers[2:4:6]fmt. PRINTLN (s)//[2, 3]fmt. Println (Cap (s))//4 "When the input operand of the slice is a slice, the capacity of the resulting slice depends on the input operand, not the underlying array it points to:" ' gonumbers: = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}fmt. Println (Cap (numbers))//10S1: = numbers[1:4]fmt. PRINTLN (S1)//[1, 2, 3]fmt. Println (Cap (S1))//9S2: = numbers[1:4:5]fmt. PRINTLN (S2)//[1, 2, 3]fmt. Println (Cap (S2))//4S3: = s2[:]fmt. PRINTLN (S3)//[1, 2, 3]fmt. PRINTLN (S3)//4 "In this example, the capacity of S3 cannot exceed the capacity of S2 (4), even if the array it points to has 10 elements, and S1,S2,S3 starts at 1. When the input operand is slice, the full expression's index high cannot exceed its cap (input) ' gonumbers: = [10]INT{0,1,2,3,4,5,6,7,8,9}S1: = Numbers[0:1]fmt. PRINTLN (S1)//[0]FMT. Println (len (S1))//1FMT. Println (Cap (S1))//10S2: = NUMBERS[0:5]FMT.PRINTLN (S2)//[0, 1, 2, 3, 4]fmt. Println (Cap (S1))//10 "In addition, for its max value, there are two additional rules * High <= max* max <= Cap (input) ' gonumbers: = [10]int{0,1,2,3,4,5, 6,7,8,9}S1: = numbers[0:1]s2: = numbers[0:5:11]//Invalid slice index One (out of the bounds for 10-element array) fmt. Println (S1, S2) "In a complete slice expression The low index is" gonumbers ": = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}s: = numbers[:4:6]fmt. PRINTLN (s)//[0, 1, 2, 3]fmt. Println (Cap (s))//6 ", however, can not omit a slightly higher index (high) ' gonumbers: = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}s1: = numbers[:4:6]s2: = s1[: : 5]fmt. PRINTLN (S2) fmt. Println (Cap (S2)) "Otherwise, the code will be in the Compile Times Error" Middle index required in 3-index slice "

via:https://medium.com/golangspec/slice-expressions-in-go-963368c20765

Author: Michałłowicki Translator: Bizky proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

133 Reads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.