Golang Basic Morphology

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

In programming languages, morphology is the code's constituent rule. That is, the lexical requirements of what we typed in order to write the compiler can recognize the code, so to master a language, then the lexical language is the first step we want to learn, the go language is the same.

    • Source Code representation

The code for the Go language is made up of several Unicode characters, and the Unicode encoding specification is a character encoding used on a computer that sets a uniform and unique binary encoding for each character in every language that exists in the world. As a result, it is able to meet cross-language, cross-platform conversion and text processing requirements, and more on Unicode, see the official website, listen to Unicode. Currently, you only need to remember one rule:

The source code for the Go language is encoded in the UTF-8 format of the Unicode encoding specification.
    • Comments

-Listen to comments: Listen to the two slash "//" start to the end of this line-listen to the block comment: Listen from "/*" listen to start to listen to "* *" end

The lexical elements of the go language include 4 classes:

    1. Marker (identifier)

    2. Keywords (keyword)

    3. Operator (operator) and delimiter (delimiter)

    4. Literal (literal)

In general, space characters, tabs, carriage returns, and line breaks are ignored unless they are part of a delimiter between multiple language symbols. In addition, the go language automatically inserts semicolons into the code for statement separation, so you do not need to display the insertion semicolon in special cases;

Marker

A marker is the name of a variable, a constant, a function, and a custom type. The name specification for the Go language marker is a sequence of characters consisting of letters, underscores "_", and numbers, and the first character of a sequence of characters must be a letter, and the first letter marker is case-sensitive. Must be declared before using a marker, a declaration that binds a non-empty marker to a constant, type, variable, function, or code package. In the same code block, it is not allowed to repeatedly declare the same marker, and the scope of the marker is the same as the range of the owning code block.

    • Predefined markers

There is a special type of marker in the go language, which is called a predefined marker. This type of marker is declared in the source code of the Go language, specifically as follows:

Basic data type name: Listen int, float, string ... Interface type name: Error constant name: True,false and Iota built-in function name: append,cap,close,complex,copy,delete,imag,len,make,new,panic,print,println , Real,recover
    • Empty identifier

The go language has a special marker named "_" called (blank identifier). Usually used as an ignore placeholder, you can make an expression lvalue, but you cannot read the content. Empty flags are also commonly used to temporarily circumvent error checking for unused variables and imported packages, but be aware that it is also a pre-defined identifier.

    • Qualifying Markers

In the Go language development process, you will often have access to variables or types in other code packages, and you need to use a qualified marker. The code package name can be referred to as a qualifier prefix, a prefix, and a token in the package in English ("."). Symbol Delimited. To apply a marker from another code package, you need to meet two prerequisites:

    1. The code package must be imported into the code file in advance

    2. The designator must be exportable in the code package

What's a sign that can be exported?

    1. The first character in a marker name must be capitalized

    2. The marker must be a variable, type, field name of struct type, function or method name, etc. declared in a code package.

Because the go language determines the access rights of the glyph based on the case of the first letter in the name of the marker, the rule is that when the first character of the marker name is lowercase, its access is "package-level private" and only the code that is in a code package with that identifier can access it. When the first character of a marker name is uppercase, its access is "public", which means that the marker can be accessed by any code package with a qualified marker.

Use an example to summarize the above content!

The package listens to the main listen to listen to hear the//main the packet name sign import listen to listen to hear and listen to hear listen and listen listen to listen to listen to the "FMT" Listen listen to listen to hear listen to listen to hear the "StrConv" listen to listening to listen to hear the "//import" Listen to the StrConv Standard code package name marker Func listen to main () listen {Listen to listen to//main listen to the function name marker listen to listen to hear a string listen to listen to the "10010" listen to listen to listen to listen to a predefined marker listen to hear V listen to hear: Listen to StrConv. Atoi (s) listen to//v custom markers, _ blank marker Listen listen to the FMT. Printf ("%T, listen to%v", listen to V, listen to V) listen to//fmt. printf listen to the qualifying marker}
Note: The code pack designator does not belong to any one scope, and the code package declaration is only to differentiate between several source code files that are part of the same code package. Or specify a reference name that represents the code package when the code package is imported.

Naming recommendations:

    1. Try to use short name, long name is not as easy to read as you think, more documents and notes the name of the foreign minister is more practical.

    2. Mix case and use hump spelling format, note the case of the first letter is willing to

    3. Keep the package name and directory consistent, try to take a short and meaningful name, do not conflict with standard library names, package names should be lowercase words, do not use underscores or mixed case

    4. function arguments and return values start with lowercase letters

    5. Do not use reserved keywords as markers

    6. It is not recommended to use the same name as a predefined constant, type, built-in function as a marker

    7. Exclusive names are usually all capitalized

Literal quantity

In computer science, a literal is a representation (notation) that is used to express a fixed base value in the source code. Almost all computer programming languages have literal representations of basic values, such as integers, floating-point numbers, strings, characters, and Boolean types that have corresponding literal representations; For example: The number 15 is unique, but it is represented in many ways:

Binary expression: 11,118 binary expression: 170 binary expression: 156 binary expression: 0xF

For the developer, of course, know the meaning of the expression, but to convey this idea to the compiler, because the compiler to the source code to do lexical analysis will automatically decide the current user input is the type of literal;

The literal of the go language should be related to the variable type:

literal type Types of variables Example
Boolean bool True/false
Character Rune The characters contained in the single quote ' '
Integral type Int 15, 017 and 0xF
Floating point Float64 0.,1.1,1.e+0.1e6,.25,.25e+5
Plural complex128
String String Enclosed in double quotes "" or "inverted quotes"

To illustrate:

The package listens to Mainimport (listen and listen to "FMT" listen to listen to "reflect") Func listen to main () listen {Listen to listen to listen I listen to: = Listen to the 0xf listen to listen to f listen to: = Listen. 25 Listen and listen to C listen: = Listen to ' C ' listen listen to listening to: = Listen to ' Go Listen to the CX listen: = Listen to 011i listen and listen to the FMT. Println (reflect. ValueOf (i), listen to reflect. TypeOf (i)) listen and listen to the FMT. Println (reflect. ValueOf (f), listen to reflect. TypeOf (f)) listen and listen to the FMT. Println (reflect. ValueOf (c), listen to reflect. TypeOf (c)) listen and listen to the FMT. Println (reflect. ValueOf (s), listen to reflect. TypeOf (s)) listen to the FMT. Println (reflect. ValueOf (CX), listen to reflect. TypeOf (CX))} output: 15 Listen int0.25 Listen to float6499 listen to the Int32go listen to string (0+11i) listen to complex128

The popular understanding of the literal: The compiler infers the data type of the input data according to the user's input.


This article from "learned in the text, about the ceremony" blog, reproduced please contact the author!

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.