Summary of basic data types in GO language

  • 2020-05-07 19:53:32
  • OfStack

This example summarizes the basic data types of the GO language. Share with you for your reference. The details are as follows:

1, comments (like C++1)

Line comment: // block comment: /*... * /

2, identifier

It is safe to say that all Unicode character combinations are acceptable except those that start with a number, those that start with a symbol, and those that don't start with a keyword. "_33" can also be an identifier, and "we" can also be an identifier. Identifiers are also case sensitive.

(1) identifiers beginning with a capital letter are public. (this is interesting)

(2) any other identifier is private.

(3) the null identifier "_" is a placeholder, which is used to discard or ignore a value during the assignment operation. Usually used like this:

go method 1 normally returns 2 values, 1 normal return, and 1 error id. If fmt.Println (x) returns two values, one for the number of bytes printed and one for the corresponding error value, then the line count,_ = fmt.Println (x) ignores the corresponding error value.

3. Constants -- declare with the const keyword

(1) types can be automatically inferred,

Such as:

const a = 5

(2) you can specify the type explicitly,

Such as:

const a int16 = 6// int16 is 1 Type of shaping 

(PS: go:

Keyword constant (variable) name type = value

(3) multiple constants can be declared once,

For example: const (a = 0; b = 2), this is called a group declaration. At this point, the first constant is set to 0 by default, and subsequent constants are set to the expression of the previous constant by default.

(PS: but if you want to use const (a b) for a=0,b=0, you can't use const (a b) for a=0,b=0.

(PPS: note that in go, 1 usually does not need a semicolon to indicate separation. The compiler automatically adds a semicolon to the end of each line.

(4) the keyword iota represents a continuous untyped integer constant. iota is 0 by default and increments gradually.

That is,

const(a = iota; b ; c )
Both b and c are now iota, so a is 0, b is 1, c is 2.

(5) in multiple assignments of one line, iota does not affect each other. Every time iota appears, its value is 0, such as:

const (
        i, j, k = 2 * iota, iota, iota + 2
        m, n, l
)

At  , the values of i and j are 0, k is 2, m, n and l are 2, 1 and 3, respectively

(PS: go language supports multiple values on 1 line)

4. Variable

Variables can be defined in two ways:

(1) one is to use the keyword var,

Such as:

var i int // Default values are automatically set here 0, If it is a string, it is null by default 

Or:

var i = 8 // Declare a value at the same time, automatically deriving its type 

Or:

var k int = 16 // Specifies the type, declares, and assigns a value 

Or:

var ( a int; b int; c int) // Group declarations, similar to constants. 

(2) the other is to use the fast variable declaration, is := operator, it will declare and initialize a variable, can automatically derive the type. However, this declaration has one limitation: it can only be used inside a function, and it will report an error outside the function.

Such as:

name := "chandler qian" // The automatic derivation type is string

Note that the := operator is declared and initialized, that is, in the same scope, the variable must be undeclared, otherwise it is an error. As follows:

k, b := 7, 8
    fmt.Printf("before k=%d,b=%d\n", k, b)
    if k := 1; k != -1 {
        b := 3
        fmt.Printf("inner k=%d,b=%d\n", k, b)
    }
    fmt.Printf("after k=%d,b=%d\n", k, b)

In the if operation below, k and b are re-evaluated with the := declaration, but no problem, since they are out of the if scope, they disappear.

The result is:

before k=7,b=8
inner k=1,b=3
after k=7,b=8

The if statement would look like this:

if k = 1; k != -1 {
    b := 3
    fmt.Printf("inner k=%d,b=%d\n", k, b)
}

Then the final output is:

before k=7,b=8
inner k=1,b=3
after k=1,b=8

As you can see, "=" is the global assignment, while ":=" creates and assigns a variable in its scope.

(3) the type of the shaping literal is automatically inferred as int, the floating point literal is automatically inferred as float64, and the complex literal is automatically inferred as complex128

5, Boolean

The Go language strictly filters the use of comparison operators ( < , < =, ==,! =, > =, > ) for comparison. The two values must be of the same type or implement the same interface. Such as:

func test0() {
    var a int = 5
    var b float32 = 4.4
    if a > b {
        fmt.Println(">")
    }
}

Compile error: invalid operation: a because a and b types do not match > b (mismatched types int and float32), take a look at the following:

func test1() {
    var b float32 = 4.4
    if b > 7 {
        fmt.Println(">")
    }
}

This is fine, though the type does not match, but b is compared to the untyped integer numeric constant 7. Untyped numeric constants can be used for any comparison.

I hope this article has been helpful to your GO programming language.


Related articles: