Go (day3[array])

Source: Internet
Author: User

array: var variable name [Len]type

Usage scenarios:

  • Fixed requirement, use array, cannot exceed specified length

  • Length is also part of the array type

  • The underlying memory space is contiguous

  • Used to store constants

  • Arrays are a special kind of dictionary .....

  • Array write, assignment method, array [index]= value

  • An array is a value type, and if you copy it into another array, the array is modified, and the original array does not change

Array initialization:

???? Defined:???? var variable name [Len]type

    • The first kind:?

      • Var?a[5]?int?=? [5]int{1,2,3,4,5}

    • The second type:

      • ceshi?:=[5]int{1,3,4,5}
    • Third Kind

      • Var?b?=? [5]int{1,3}
    • The fourth type (when the unknown length is used ...), it is important to note that this usage is also in the slice, but the meaning is different

      • Var?c?=? [...] int{1,3,4,5,10}

?

Example:

Var?a[5]intvar?a[5]stringvar?a[5]float32var?a[5]bool

To access an element in an array:

? Using subscript access:? A[0] accesses the first element. An array of length 5 is accessed as follows: A[0], a[1], a[2], a[3], a[4]

The length in the array:

???? var a[10]int

???? Lengh: =len (a)

The sample code splits the section:

  1. Array loop:

    1. format%p, used to print memory address

    2. & symbol, take variable address

    3. Try using variables to loop

    4. var?a?[ 10]intfor?i?:=0;i<len (a); i++{??? Fmt. Printf ("%p\n", &a[i])} 
    5. length:=len (a)? Array length 


  2. Array initialization:

    1. Creates another array, modifies the array, and does not modify the original array

    2. func?test2 ()?? {??? Var?a[5]int?? =? [5]int? {1,2,3,4,5}??? Var?b[5]?int??? B?=?a??? Fmt. Printf ("%v\n", b)??? B[0]?=?200??? Fmt. Printf ("%v\n", b)??? Fmt. Printf ("%v\n", a)} 
  3. Array definition:

    1. when declaring an array, you need to define the length and type!!!

    2. array Assignment: Specify subscript to specify element assignment

    3. % #以go的方式打印

    4. var?d?[ 5]string?=? [5]string{1: "ABC", 4: "Zcq"}//%#?? Print to go!!! Fmt. Printf ("%#%v\n", D) fmt. Printf ("%v\n", a,b,c) 
  4. Two-dimensional array

    1. func?test4 ()?? {??? Two-dimensional array??? Var?a[4][2]int??? for?i:=0;i<4;i++{?????? for?j:=0;i<2;j++{????????? A[i][i]?=? (i+1)? * (j+1)??????}???}??? for?i:=0;i<4;i++{?????? for?j:=0;i<2;j++{????????? Fmt. Printf ("%d", a[i][j])??????}?????? Fmt. Println ()???}}

  5. Array Assignment: Random numbers into the array

    1. Remember: Go is a strongly typed language, so if the type is different, it is not compiled

    2. using the random number module, use the time module

      1. r?: =?rand. New (Rand. Newsource (time. Now (). Unix ()) 
    3. func?test5 ()?? {??? Random numbers into the array??? R?:=?rand. New (Rand. Newsource (time. Now (). Unix ()))??? Var?five? [100]int??? Array? To assign a value by index??? For?i:=0;i<len (five); i++{?????? Assignment Operation?????? Five[i]=r.int ()?????? Value Operation?????? Fmt. Printf ("%d\n", Five[i])???}??? Fmt. Printf ("%d\n", Five)} 
  6. Array assignment: Random string into array

    1. Go inside is utf-8 code

    2. Sprintf format and return the formatted string, can be directly assigned value!!!

    3. Func?test6 ()?? {?????? Var?a? [100]string??? Var?b?string?=? " Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz "??? For?i:=0;i<len (a); i++{?????? Var?str?string?????? For?j:=0;j<len (b); j++{????????? Index?:=rand. INTN (len (b))????????? Sprintf? format and return the formatted string????????? Str?=?fmt. Sprintf ("%s%c", Str,b[index])??????}?????? A[i]?=?str?????? Fmt. Printf ("a[%d]=%s\n", I,a[i])???}}



Go (day3[array])

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.