Go language identifiers, keywords, literals, types

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

has been in Segment Fault above the implementation of their own take doctrine, but in fact, I have always been very happy to share the person, and especially like to write, has been in their own blog, but no one to see, also form communication, so, application in Segment Fault above a column, Later also forget everyone advice, this article just want to try Segment fault editor, the content is written a few days ago.

The language symbols of the go language are also called notation elements, including 5 categories, tag (), identifier keyword (), keyword operator (), delimiter (), and operator delimiter literal ( literal ), which are the basic units of the Go Language code and program.

All source code for the go language must be encoded Unicode in the UTF-8 encoded format of the encoding specification.

First, identifiers

In the Go language code, each identifier can represent a change or a type (that is, the identifier can be considered a variable or type's code or name), the identifier is a sequence of characters consisting of several letters, underscores (_), and numbers, and the first character must be a letter. At the same time, an identifier must be declared before it can be used. In a code block, duplicate declarations of the same identity are not allowed.

The Code Package Declaration ( package PKG_NAME ) is not a declaration because the code package name does not appear in any one scope, and the purpose of the code package declaration statement is only to identify whether several source files belong to the same code package, or to specify the default code package reference name when the package is imported.

A qualified identifier represents access to an identifier in another code package, which requires two conditions:

  1. Another code package must be imported by the import declaration of the Go language import ;
  2. An identifier is exportable in the code package.

The following are the two prerequisites for an identifier to be exported:

  1. The first character in the identifier name must be uppercase;
  2. The identity must be the name of a variable or type that is declared in a code package, or a field name or method name that belongs to a struct type.

In the go language there is also a class of special identifiers, called Predefined identifiers, which appear along with the source of the go language, mainly including the following:

    1. Name of all base data types
    2. Interface typeerror
    3. Constants true , false andiota
    4. Names of all the mole functions, i.e.,,,,,,,,,,,,,,, append cap close complex copy delete imag len make new panic print println real andrecover

The identifier, represented by an underscore, is a null identifier, which is typically used in a new binding declaration that needs to be introduced, such as:

import _ "runtime/cgo"

Second, the key word

A keyword is a sequence of characters that is reserved by a programming language to keep a page from being used by programmers as identifiers. Therefore, the keyword is also called a reserved word.

All the keys in the Go language are only 25:

    1. Program declaration: import ,package
    2. Program entity declaration and definition:,,,,,,, chan const func interface map struct typevar
    3. Program Flow control:,,,,,,,,,,,,,, go select break case continue default defer else fallthrough for goto if range returnswitch

Third, the literal quantity

In simple terms, the literal is a notation of the value, but in the go language, the meaning of the literal is much broader:

    1. The various literals used to represent the underlying data type values.
    2. The user constructs a type literal of various custom composite data types, as the following literal represents a custom struct type called Person:

      type Person struct {     Name string     Age uint8     Address string}
    3. A compound literal used to represent a value of a composite data type, or, more precisely, a value that is used to construct a type struct (struct), array (array), Slice (slice), and map (dictionary). The following literal can represent the value of the person struct type above:

      Person(Name: "Eric Pan", Age: 28, Address: "Beijing China"}

Iv. types

A type determines the set of values and the actions that can be exerted on those values. A type can be specified by a type name or type literal, a type is divided into a base type and a composite type, and the name of the base type can represent itself, such as:

String is a basic type, and the basic types in the Go language are: bool, byte, Int/uint, Int8/uint8, Int16/uint16, Int32/uint32, Int64/uint64, float32, Float64, complex64, complex128, a total of 18, the name of the base type must be a predefined identifier. In addition to bool and string, the others are called numeric types.

In addition to the basic types, the go language has eight composite types: Array (array), struct (struct), function (functions), Interface (interface), Slice (slice), Map (dictionary), Channel (channels) and pointer (pointers).

A composite type is typically composed of several (including 0) other defined types, such as a structure that defines a book:

type Book struct {     Name string     ISBN string     Press string     TotalPages uint16}

The types in the go language can also be classified as static types and dynamic types, and the static type of a variable refers to the type shown in the variable declaration, and most types of variables have only static types, except for the variable of the interface type, which has a dynamic type in addition to the static type. This dynamic type represents the actual type of value that is bound to the variable at run time.

Each type will have a potential type, if the type is a predefined type (that is, the base type), or a composite type constructed by a type literal, then its potential type is itself, such as a string type of potential type, the book mentioned above The potential type is book, but if a type is not the case, then the potential type of this type is the potential type of that type in the type declaration, for example, we declare a MyString type as follows:

type MyString string

The potential type of the MyString type is a potential type of string, in fact, we can treat MyString as an alias of type string, which is the basic data type Rune type in the Go language, which can be thought of as an alias type of the UInt32 type , its potential type is uint32, but one must note that MyString and string are not of the same type.

The potential type is transitive during the declaration, as we declare a istring type below:

type iString MyString

The potential type of the

Istring type is also the string type.

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.