Golang Study NOTES: statement and scope of language specification

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

Type declarations include: constants, custom types, variables, functions, labels, and package declarations.
Identifiers in the program must be declared. Identifiers in blocks, packages, and files cannot be duplicated.

Declaration   | TypeDecl | VarDecl .TopLevelDecl  | FunctionDecl | MethodDecl .

Label Scope

tags are used in tag statements, often in break, continue, goto statements. It is illegal to define labels but not to use them. Labels are non-block scopes and do not conflict with other non-tag identifiers compared to other identifiers.

Pre-declared identifiers

The identifier below is reserved in Golang

Types:BOOL byte complex64 complex128Errorfloat32 float64    int int8 Int16 Int32 Int64 Rune string    UINT uint8 uint16 UInt32 UInt64 UIntPtrConstants:true false IotaZero Value:NilFunctions:Append Cap Close Complex Copy Delete Imag Len     Make New Panic Print println Real Recover

Can export methods or fields

An identifier can be exported for another package to access, only to meet

    • Capitalize first letter
    • The field or method declared in the package

Constant declaration

Use the Const keyword

ConstDecl   = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) ConstSpec    = IdentifierList [ [ Type ] "=" ExpressionList ] IdentifierList = identifier { "," identifier } .ExpressionList = Expression { "," Expression } .
ConstPifloat64=3.14159265358979323846ConstZero =0.0         //untyped floating-point constantConst(SizeInt64=1024x768EOF =-1  //untyped integer constant)ConstA, b, C =3,4,"foo"  //A = 3, B = 4, c = "foo", untyped Integer and string constantsConstU, vfloat32=0,3    //U = 0.0, v = 3.0

Itoa (enumeration)

Itoa represents a continuous, untyped constant, calculated from 0, and reset to 0 when another itoa appears.

Const(//Iota is reset to 0C0 =Iota  //C0 = = 0C1 =Iota  //C1 = = 1C2 =Iota  //C2 = = 2)Const(A =1<<Iota  //A = = 1 (iota has been reset)b =1<<Iota  //b = = 2c =1<<Iota  //c = = 4)Const(U =Iota* the  //U = = 0 (untyped integer constant)Vfloat64=Iota* the  //V = = 42.0 (float64 constant)W =Iota* the  //W = = (untyped integer constant))Constx =Iota  //X = = 0 (Iota has been reset)Consty =Iota  //Y = = 0 (Iota has been reset)

Type declaration

Use type to define a new type

typedecl="Type"(TypeSpec|"("{TypeSpec ";"}")") .TypeSpec= IdentifierType. type intarray [16]int type (point  struct{ x, y float64 } Polar 
     
      point)
       type TreeNode struct { left,  right *TreeNode  Value *comparable}      type Block interface { BlockSize() intEncrypt(src, DST []byte) Decrypt(src, DST []byte)}     

Custom types do not inherit methods of the original type, but elements of an interface method or a combination type retain the original method.

//A Mutexis a Data type with the methods, Lock and Unlock. type Mutex struct {/* mutex  fields */} func(M *Mutex)Lock()    { /*LockImplementation */}func(M *Mutex)Unlock()  { /*UnlockImplementation */}//NewmutexHas the same composition as MutexBut it method set is empty. type newmutex Mutex // theMethod set ofThe base type of Ptrmutex remains unchanged,But the method set of Ptrmutexis empty. type ptrmutex *Mutex // theMethod set of*PrintablemutexContains the methods//LockandUnlockBound to its anonymous fieldMutex. type printablemutex struct { Mutex} //Myblockis an interface type that has the same method set as Block. type myblock Block 

Custom types can be used for Boolean, numeric, or string types, as well as for attaching methods to them

typeintconst (    EST TimeZone = -(5iota)    CST    MST    PST)funcstring {    return fmt.Sprintf("GMT%+dh", tz)}

Variable declaration

A variable declaration represents the creation of one or more variables, and then binds the related type to it, and assigns it to the initial value.

VARDECL ="var"(Varspec |"("{Varspec";"}")") . Varspec = Identifierlist (Type ["="Expressionlist] |"="expressionlist).varIintvarU, V, Wfloat64varK =0varX, yfloat32=-1,-2var(IintU, V, s =2.0,3.0,"Bar")varRe, im = Complexsqrt( -1)var_, found = Entries[name]//Map lookup; Only interested in "found"

If the variable is declared but not used, the compiler will error

Short variable Declaration

Short variable declaration syntax

":=" ExpressionList .i, j := 0, 10funcintreturn 7make(chanint)r, w := os.Pipe(fd)  // os.Pipe() returns two values_, y, _ := coord(p)  // coord() returns three values; only interested in y coordinate

However, a short variable declaration can be declared only inside a function, but in an if, for, switch statement as a temporary variable.

function declaration

The syntax is as follows

"func"FunctionSignatureFunction     SignatureBlock

Method declaration

The method is a function with the recipient. The syntax is as follows

MethodDecl   "func"FunctionSignature ) Receiver     = Parameters

Recipient can be a T or *t type

func  (P *Point) Length () float64  {return  Math. SQRT (p.x * p.x + p.y * p.y)}func  (P *point) scale (factor  Float64 ) {p.x *= factor p.y *= factor}  
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.