Golang Starter Series (three) Go basics of the language summary

The basic structure 1. Go program

Below is the basic structure of a Go program, comprising (package statement, the package is introduced, functions, etc.)

Copy the code
package main // define the package name, package main program represents an independent execution of each Go application contains a package called main. 

import "fmt" // need to use imported packages (function, or other elements) 

FUNC main () {// entry function program. Each function is main executable program must contain, in general are a function of the start of the first execution. 
  fmt.Println ( "the Hello, World!") 
}
Copy the code

 

2. Data Types

Basic data types including basic go Built-in data types and advanced data types

1. The basic data types
  Boolean type (bool)

  Numeric types

    Integer type (byte, rune, int / uint , int8 / uint8, int16 / uint16, int32 / uint32, int64 / uint64)
    floating point type (float32, float64)
    type complex (complex64, complex128)

  String type
    string Go strings are connected by a single byte. Identified using Unicode UTF-8 text encoding.

2. Advanced Data Types

  An array (Array)
  slice (Slice)
  Dictionary (Map)
  channel (channel)
  function (function)
  structure (function)
  the interface (interface)
  a pointer (* Xxx, Pointer, uintptr)

If the underlying structure is divided according to the value of the type comprising (all the basic data types, arrays, structures), comprises a reference type (slice, map, channel, function, interface, pointer)


3. Variables & Constants

  1. The variable names letters, numbers, underscores, wherein the first letter is not digital, for example: var String name
  2. statement
    . A specifies the variable type, after the assignment statement if not, the default values.
      String name var
      name = "John Doe"

    B. The value of the decision variable type itself.
      var name = "John Doe"

    C short form, var omitted, attention. 
      Age: = 10

  Note
    (: =) is the preferred form of the variables used
    (: =) can be used in the body of the function, it may be used instead of the global variables statements and assignments.
    (: =) To the left of the variables it should not be declared in place, otherwise it will cause a compiler error.


  3. The constant data types can only be boolean, numeric (integer, floating point and complex) and string.
    1. constant declaration: B const String = "ABC"
    2 IOTA, special constant
      const (
        A = IOTA
        B
        C
      )

    1 represents the continuous, non-integer constant type,
    2. const constant declaration statement starting unit,
    3. from 0, not assigned to a time constant incremented
    4. once across the constant declaration statements to return to the beginning const 0

4. operators
  1. arithmetic operators, a + b, including (+, -, *, /,%, +, -)
  2 relational operator returns True or False, a == b, comprising (==, =,>, <,> =, <=)!
  3. logical operators, returns True or False, comprising (&&, || ,!)
  4. address operator 

    &: Return variable storage address (& originalValue)

    *: Pointer variable (* pointerValue)

  The receiving operator, or a channel for receiving data is added to the data channel (intChan <-1, <-intChan)


5. Error Processing

  1. error Interface, (errors.New (value), fmt.Error (), custom error type)

Copy the code
func Divide(a, b float64) (result float64, err error) {
    if b == 0 {
        result = 0.0
        err = errors.New("runtime error: divide by zero")
        return
    } else {
        result = a / b
        err = nil
    }
    return
}
Copy the code


  2. panic function, panic (value) and the error associated with the interface,

    var user = os.Getenv("USER")
    if user == "" {
        panic("The USER environment variable is not set.")
    }

 

  3. recover function, and defer statement in conjunction with,

Copy the code
TestB, FUNC () (ERR error) { 
    the defer FUNC () {// when an abnormality occurs, provided the recovery 
        IF X: = Recover (); X = nil {! 
            ERR = fmt.Errorf ( "Internal error:% V", X) 
        } 
    } () 

    panic ( "FUNC TestB, (): panic") 
}
Copy the code

 

6. Go basic commands

  go build command is mainly used to test compilation. During compilation package, if necessary, will also compile the package associated with it.

    go run hello.go

  go get command is mainly used to dynamically obtain remote code package.

    go get github.com/go-sql-driver/mysql

  go run command is used to compile and run the Go program.

    go run hello.go

  go test command, it will automatically read the source directory of files named * _test.go, build and run the executable file of the test.

 

Guess you like

Origin www.cnblogs.com/fengchuiyizh/p/12299579.html