Array of Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang uses array to represent fixed-size arrays, and slice to represent dynamic arrays.

Package Mainimport "FMT" Func Main () {var a = [5]int{2,3,5,7,11}fmt. Printf ("type of array:%t\n", a) fmt. Printf ("Address of Array:%p\n", &a) fmt. Printf ("Address of arrar[0]:%p\n", &a[0]) s: = A[:]fmt. Printf ("slice =%v, Len =%d, Cap =%d\n", S, Len (s), Cap (s)) fmt. Printf ("Address of Slice:%p\n", &s) fmt. Printf ("Address of slice[0]:%p\n", &s[0]) s = append (s), FMT. Printf ("slice =%v, Len =%d, Cap =%d\n", S, Len (s), Cap (s)) fmt. Printf ("Address of Slice:%p\n", &s) fmt. Printf ("Address of slice[0]:%p\n", &s[0])}

The output is as follows:

Type of array: [5]int
Address of array:0xc420014150
Address of arrar[0]: 0xc420014150
Slice = [2 3 5 7], Len = 5, Cap = 5
Address of slice:0xc42000a060
Address of slice[0]: 0xc420014150
Slice = [2 3 5 7], Len = 6, Cap = 10
Address of slice:0xc42000a060
Address of slice[0]: 0xc42001c0f0

Resources:

Slices:usage and Internals

https://golang.org/pkg/builtin/#append

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.