Go Language Learning Notes

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

The array, slice, and map in Go

    1. Array
      Definition of an array
var arr [Ten]int//definition array arr, default initial values are0arr[0] =1Change the value of the first element of the array to1Arr: = [3]int{1,2,3} arr: = [...]int{1,2,3}//Note...That represents the size of the array by the compiler itself/* Multidimensional array */arr: = [3][2]int{{1,2},{2,3},{3,4}}//reasonable arr: = [...][2]int{{1,2},{2,3},{3,4}}//Reasonable/* arr: = [3][...]int{{1,2},{2,3},{3,4}} Error */
    1. Slice
      is equivalent to a slice inside python, but there are differences. The slice in Go is a pointer to an array and is a reference type. This means that when a slice is assigned to another variable, two references point to the same array. If a function requires a slice parameter, modifications to the slice element in it are also reflected in the function call, similar to the pointer passing the underlying array.
10)    // 创建一个保存有十个元素的slice/* slice总是与一个固定长度的array成对出现,其影响slice的容量和长度*/arr := [...]int{1,2,3,4,5}s1 := a[2:4]     // 创建序号为2-3(不含4)的slices2 := a[:]        // 创建一个与arr等长的slices3 := s[:4]       //用slice 创建slice

Go keyword

Break
default func Interface Select
Case Defer Go Map struct
Chan Else Goto Package Switch
Const Fallthrough If Range Type
Continue For Import Return Var

Control structure

    • If, else
/*  用法: 除了条件不需要括号 ,其它和C一样*/if condition{    //...if-elseif-else 结构if codition1{    //...}elseif codition2{    //...}else{    //...}
    • For, continue, break
for init;condition;post{}         // 常规用法forcondition{}                    // 相当于C语言的whilefor{}                          // 死循环/* continue 和 break 的用法和C语言相似 */
    • switch, default, Fallthrough
/ * Usage one * /Switch Exp{ CaseCONS1:EXP1     CaseCONS2:EXP2    //...     CaseCONSN:EXPN Default:exp_}/ * Usage two * /Switch{//switch does not have an expression, it executes the part of true in the case expression     CaseCOND1:EXP1    //...     CaseCONDN:EXPN}/ * Switch statement using the Fallthrough keyword * /Switchi{//If i==0, it does not match down to execute EXP1     Case 0: Case 1:EXP1   //EXP1 will not be executed}Switchi{//If i==0, it will execute EXP1     Case 0: Fallthrough Case 1:EXP1   //EXP1 will be executed}
    • Map, Range
      The keyword map can be thought of as an array indexed by a string
salary: =map  [string  ]int  { "Jane"  :3000 ,  :2500 ,  "Marry"  :1800 , //the last comma must have } for  key, value: = range  salary{/* is a bit like a python dictionary */ FMT. Printf ( "%s\t%d\n" , key, value)}[output]>>>jane   Tony  2500  Marry  1800  delete  (Salary,  "Jane" ) //built-in function delete delete element "Jane"  

Built-in functions

Close New Panic Complex
Delete Make Recover Real
Len Append Print Image
Cap Copy println
    • Append, copy
/ * Append for appending slice * /S0: = []int{0,0}S1: =Append(S0,1)//Append element 2 to S0 end, S1 = = []int{0, 0, 1}S2: =Append(S1, 2, 3, 4)//Append multiple elementsS3: =Append(S2, S0 ...)//Append a slice, note the back three points/ * Copy for copying slice, returning the number of copied elements * /Arr: = [...]int{1,2,3,4,5,6,7}vars = Make([]int,5)//Create a type of []int, save 5 elements of sliceN: =Copy(S, arr[:])//n==5, the value within S is A[:5]
    • close
      for channel communication, use it to close channel
    • Delete
      for deleting instances in map
    • cap, len
      Cap return tile capacity
      Len To return strings, slice, and arrays of length
    • new, make
      New for various types of memory allocations
      make ([]t, Len, CAP) for memory allocations of built-in types
    • panic, recover
      for exception handling mechanisms
    • complex, Real, imag
      for complex numbers
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.