go language - variables and constants

variable

First, the variable Notes

  Variable names capitalized, other packages can be accessed call (public), the first letter lowercase variable names, and other packages can not access the call (private)

  In the same field in a variable can be defined only once and can not be redefined

Second, ways to declare variables

  1, after the first assignment statement

    Keyword Variable name Variable type - >> var a int

    Variable name = value - >> a = 10 - assignment requires the function body

        2, and the assignment statement

    Keyword = Value Variable Type Variable Name - >> var a int = 10

  3, derivation type

    Keyword variable name = value - >> var a = 10

  4, a brief statement and assignment, only the function body

    Variable name: = value - >> a: = 10

  5, the anonymous variable: means for receiving unwanted return value of the function is generally used, a _

Third, the batch statement, the following

  where (

    a int 

    b int =10

    c float

)

constant

First, constant declarations, must be assigned declaration

  Keyword variable name = value - >> const a = 20

Second, the bulk assignment and precautions

  If a plurality of constants constant back unassigned, assigned default values ​​of the constants a, as

  const (

    a = 10

    b =20

    c // c is 20 at this time, taking the value of the variable thereon

)

Three, iota enumeration

  Constant counter, can only be used in constant expressions, and with the use of const, plus 1 for each additional line cumulative, including blank lines, met const reset to 0, the assignment will encounter _ just do not show

  const(

    a = iota   //a=0

    b = 100  b =100

    _             //iota=2

    d            //d=3

)

  const(

    a,b =iota+1,iota+2   //iota=0,a=1,b=2

    c,d       //iota=1,c=2,d=3

    e,f                         //iota=2,e=3,f=4

)

  Defined magnitude

  const(

    _ = iota

    KB = 1 << (10 * iota) // 1 << 10 left 10

    MB=1<<(10*iota)  //1<<20

    GB=1<<(10*iota)  //1<<30

    TB=1<<(10*iota)  //1<<40

    PB=1<<(10*iota)  //1<<50

)

 

Guess you like

Origin www.cnblogs.com/puti306/p/11407390.html