Go Language Summary (1)--Basic knowledge

Source: Internet
Author: User

1. Annotations (same as C + +)

Line Comment://Block Comment:/* ... */

2. Identifiers

It can be said that, in addition to the beginning of the number is not allowed, the beginning of the symbol is not allowed, the keyword is not allowed, other Unicode character combinations can be. "_33" can also be an identifier, "us" or an identifier. Identifiers are also case-sensitive.

(1), an identifier that begins with a capital letter is public. (This is interesting)

(2), any other identifiers are private.

(3), the empty identifier "_" is a placeholder for assigning operations, discarding, ignoring a value. This is usually done with:

The Go method typically returns 2 values, a normal return, and an error ID. such as FMT. PRINTLN (x) returns 2 values, one for the number of bytes printed and one for the corresponding error value, then Count,_ = FMT. PRINTLN (x) This line of code ignores the corresponding error value.

3, constants--declared with the const keyword

(1) The type can be inferred automatically,

For example: const A = 5

(2) You can explicitly specify a type,

such as: const a int16 = 6//Int16 is a type of shaping

(Ps:go language constants, variables are defined in the following formats:

Keyword constant (variable) name type = value)

(3) You can declare more than one constant at a time,

such as: const (a = 0; b = 2), which is called a grouping declaration. At this point, the first constant is set to 0 by default, and subsequent constants are set to the expression of the preceding constant by default.

(PS: But want to use a const (a B) to denote a=0,b=0 such is not possible, constant grouping definition, the first constant must be assigned, or iota)

(PPS: Note that the go language generally does not require semicolons to denote separation, the compiler will automatically add a semicolon at the end of each line, of course, there is no mistake to add a semicolon, but the automatic formatting of the IDE like Liteide will usually help you to remove the semicolon and then help you to change the line)

(4) The keyword Iota represents a continuous, untyped integer constant, iota defaults to 0, and increments incrementally.

That is, const (a = iota; b; c) indicates that both B and C are iota at this time, so a is 0,b for 1,c 2.

(5) A row of multiple assignments, iota does not affect each other. each time iota appears, it has a value of 0, such as:

Const (        22        m, N, l    )

at this point, the values for both I and J are 0,k values of 2,m, N, and L are 2, 1, and 3, respectively .

(Ps:go language supports multiple values on one line)

4. Variables

There are 2 ways to define variables:

(1) One is to use the keyword VAR,

For example: var i int//This will automatically set the default value of 0, if it is a string, the default is null

Or: var i = 8//Declare simultaneous assignment, automatically deduce its type

Or: var k int = 16//indicate type, declare and assign value

Or: Var (a int; b int; c int)//group declaration, similar to constant.

(2) The other is declared with a fast variable, that is, by using the: = operator, which declares and initializes a variable that can be automatically inferred from the type. For example: Name: = "Chandler Qian"//Auto derivation type is string

(PS:: = operator is declared and initialized , that is, the variable must not be declared, or it is an error)

(3) The type of the shaping literal is automatically inferred to be int, the floating-point literal is automatically inferred as float64, and the plural literal is automatically inferred as complex128

5, Boolean

The go language strictly filters the values that are compared using comparison operators (<, <=, = =,! =, >=, >). The two values must be the same type or implement the same interface. Such as:

func test0 () {    varint5    var4.4    if A > b {        fmt. Println (">")    }}

Because the A and B types do not match, the compilation error is: invalid operation:a > B (mismatched types int and float32), and then look at this:

func test1 () {    var4.4    if7  {        fmt. Println (">")    }}

This is possible, although the type does not match, but B is compared to the untyped numeric constant 7. A numeric constant of no type can be used for any comparison.

Go Language Summary (1)--Basic knowledge

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.