Go language Learning-arrays and slices

Source: Internet
Author: User

Package Mainimport "FMT" Func Main () {//array var a = [3]int{}//equivalent to [3]int{0,0,0}a[0] = 1changeArray (a) fmt. Println (a) b: = [...] The int{1,2,3}//ellipsis notation allows the compiler to automatically calculate the length of an array based on subsequent initialization, but this length is the FMT determined at compile time. Println (b) C: = new ([3]int)//new returns an address, but can be accessed as Pointname[index], but the output needs to fetch the address corresponding to the value c[1] = 17//here and (*C) [1] equivalent, but not like C operation, unless using the unsafe module FMT. Println (*c)///////////////////////////////////////////////////////////////slice D: = []int{0,0,0}//similar to array but not specified length d[0] = 1fmt. Println (d) E: = make ([]int,5,10)//make can only create Slice,map,chanel and return a reference, new can create any type, return address e[1] = 12fmt. Println (e) changeslice (e) fmt. PRINTLN (e) F: = *new ([]int)//New can create any type and return an address, but cannot specify size with parameters, this method is very infrequently used F = append (f,1,2,3) fmt. Println (f) G: = c[:]//Create a slice based on an array, this method is very simple and practical [:] means that all elements can also be used [M:] [: n] [m:n]//Once a slice operation of an array returns the Slice FMT. Println (g) The}//array is a value reference, so the modification of the function array does not affect the original value and [3]int and [4]int is different type//if the value of the array must be changed, the requirement explicitly specifies a pointer to the passed array///For example here with Changearray (x *[3] int), called with Changearray (&a) func changearray (x [3]int) {x[1] = 100}//Slice is a reference, so the function changes the slice by affecting the original value func changeslice (x []int) { X[1] = 100} 

Go language Learning-arrays and slices

Related Article

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.