Golang array, slice, string note

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed. I was going to write a note about Golang io, but I had to understand the array, slice, string concepts before I learned IO, so I'll write Golang io in the next post. Array: The lengths of the arrays are part of the array type, such as var buffer [256]byte type is [256]byte, len (buffer) always returns 256. Slice: A slice describes a contiguous part of an array, such as var slice = buffer[100:250]. Slice can also be generated from slice, such as var slice2 = Slice[5:10], where the type of slice is []byte. Slice doesn't really store data, it just contains a pointer that points to the data, and you can think of it as such a structure:
Type sliceheader struct {    Length        int
    Capacity      int    zerothelement *byte}
Where capacity equals the size of the array referred to by slice minus the index of zeroelement. We can also use the Make function to create slice,slice: = makes ([]int, 10, 15) where the second parameter is the length of the slice, and the third parameter is the capacity of slice. or with direct volume slice: = []int{0, 1, 2, 3, 4}.
String: You can think of string as a read-only byte slice. For example, var sample string = "\xdb\xb2\x3d" is a byte slice with a length of 3. For Unicode data where non-ASCII is formatted as UTF-8. A Code point is a Unicode value that consists of several bytes. A character character can consist of several code points, or even different code points. Golang uses rune to represent code point, type Int32, and character in Golang normalize into a rune.

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.