Golang: types variables constants

  • 2020-05-27 05:51:24
  • OfStack

Basic types of

1. List of basic types


type         The length of the      instructions
bool         1      true/false, The default false, Can't take the 0 Value as true( No Numbers true/false)
byte         1      uint8 The alias
rune         4      int32 Alias. On behalf of 1 a unicode code point
int/unit            1 To the platform you're running on, 32bit/64bit
int8/uint8   1     -128 ~ 127; 0 ~ 255
int16/uint16 2     -32768 ~ 32767; 0 ~ 65535
int32/uint32 4     -21 Hundred million ~ 21 Million, 0 ~ 42 Hundred million
int64/uint64 8 float32      4     Accurate to 7 Decimal places , The equivalent of c the float
float64      8     Accurate to 15 Decimal places , The equivalent of c the double complex64    8
complex128   16 uintptr            Enough to save the pointer 32 A, 64 An integer , Pointer to the ( The integer type of the pointer can be stored )
array              Value types , An array of
struct             Value types , The structure of the body
string             Value types , String type, commonly used
slice              Reference types , slice
map                Reference types , The dictionary
channel            Reference types , channel
interface          The interface type , interface
function           Function types , function

2. Type conversion

Implicit type conversion is not supported and must be done explicitly

Conversions occur only between two mutually compatible types: the various int types do not allow for mutual assignment or manipulation, which would result in errors at compile time


<type>(expression)

The sample

package main
import "fmt" func main(){
    a := 0x1234
    b := 1234.56
    c := 256     fmt.Printf("%x\n", uint8(a))
    fmt.Printf("%d\n", int(b))
    fmt.Printf("%f\n", float64(c))
}

The results of

34
1234
256.000000

3. Type aliases


type t_str string
var b t_str = "a str"

4. Type default

Declaration does not assign value, type zero value, not null value, but the default value after the declaration


bool: false
integers: 0
floats: 0.0
string: ""
pointers,functions,interfaces,slices,channels,maps: nil

Reserved words


break      case   chan     const        continue
default    defer  else     fallthrough  for
func       go     goto     if           import
interface  map    package  range        return
select     struct switch   type         var

variable

1. Variable declaration


// The first 1 Type, specify the variable type, declare if not assigned, use the default value
var v_name v_type
v_name = value // The first 2 The type of a variable is determined by its value
var v_name = value // The first 3 Kind, omit var, Pay attention to := The variable on the left should not be declared, otherwise it will cause a compilation error .
v_name := value e.g.
var a int = 10
var b = 10
c : = 10

Example:


package main
var a = 1234
var b string = "hello"
var c bool func main(){
    println(a, b, c)
}

Results:


1234 hello false

2. Multivariate declaration:


<type>(expression)
0
Example:

<type>(expression)
1
Results:

0 0 0 false 1 2 123 hello 123 hello

Note:

A. In multi-variable assignment, the values of all variables on the left side will be calculated before the assignment


    i := 0
    i, l[i] = 1, 2
    //get i = 1 . l[0] = 2
    sc[0], sc[0] = 1, 2
    //get sc[0] = 2

Garbage can _

<type>(expression)
4

C. Declared but unused variables report errors at compile time, which is more stringent than Python

constant

Constants can be characters, strings, booleans, or Numbers

Constant assignment is a compile-time behavior

1. Constant declarations

A value that can be determined at compile time and cannot be changed at run time
Constants can be defined as types such as numeric values, Boolean values, or strings


<type>(expression)
5

Description:


<type>(expression)
6

The enumeration

iota, a special constant, can be considered a constant that can be modified by the compiler

Each time the const keyword appears, it is reset to 0, and then every time the iota keyword appears before the next const, the number represented by iota will automatically increase by 1

No initial value is provided, indicating the use of the expression on the previous 1 row

1. The statement:

iota generates automatically growing enumeration values from 0, meaning that one more enumeration value, iota+=1, is used or not

The basic grammar


<type>(expression)
7

iota usage


<type>(expression)
8

Note: the number of variables per line must be 1 to const (A, B = iota, iota C, D E, F)


<type>(expression)
9

The operator

The Go operator is all combined from left to right

Operator overloading is not supported


package main
import "fmt" func main(){
    a := 0x1234
    b := 1234.56
    c := 256     fmt.Printf("%x\n", uint8(a))
    fmt.Printf("%d\n", int(b))
    fmt.Printf("%f\n", float64(c))
}
0
In go, ++ -- is a statement, not an expression

package main func main(){
    i := 1
    i ++
    println(i)     b := i
    println(b)     //syntax error: unexpected ++, expecting semicolon or newline or }
    //c := i++
    // means , ++/-- It doesn't appear on the right-hand side
}

Pointer to the

Go retains Pointers, and *T represents the pointer type corresponding to T

If the package name is included, it should be *.T

The symbol '*' representing the pointer type is always placed at 1 with the type, not next to the variable name

Pointer **T also supports Pointers

1. The statement


var a, b *int

2. Show

The operator & Take the variable address, and use * Indirectly access the target object through pointer variables
The default value is nil, There is no NULL constant
Does not support pointer operation, does not support ' ->' Budget bliss, direct '.' Selectors operate on pointer target object members
Can be found in unsafe.Pointer Converts between Pointers of any type
Can be unsafe.Pointer convert uintptr, And then do pointer operations in a disguised way, uintptr It can be converted to an integer

Example 3.


package main
import "fmt" type User struct {
    Id int
    Name string
}
func main(){
    i := 100
    var p *int = &i  // Take the address     println(*p)   // The values
    up := &User{1, "Jack"}
    up.Id = 100  // Direct fetch is only for members
    fmt.Println(up)     u2 := *up  // Copy the object
    u2.Name = "Tom"
    fmt.Println(up, u2)
}

4. Results:


100
&{100 Jack}
&{100 Jack} {100 Tom}

Grouping statement

import (
    "fmt"
    "os"
) const (
    i = 100  // The first line must have a constant expression
    pi = 3.1415
) var (  // Global variables are available, not supported in the body of the function
    i int
    pi float32
)


Related articles: