Go variables and constants

Variable declaration

  1. Define the variable type specified when the variable formula

  2. Define the variable type inference rely upon

    go static, strongly typed languages ​​(java, c ++, too).

    Static language: a variable can be determined at compile-time type of language
    dynamic languages: determine the variable type of language runtime
    strongly typed languages: Once you determine the type, run-time can not be changed, even if the cast does not alter the original variable type
    is weak type: the context is able to run the implicit data type conversion

  3. Variable names brief statement left with at least one new, global variables can not be defined

    1. Variable name on the left there is at least one new

    2. You can not define global variables

  4. Multi-variable declaration

    var a,b,c int = 1,2,3
    var a,b,c = 1,2,3
    var(
     name = ""
     age = 23
     sex = ""
    )
  5. Zero value

    1. Empty Analyzing a == nil
    2. int 0 float 0.0 string ""
    3. Slices are reference types, a zero value is nil
  6. Variables must be defined, otherwise an error

Constant declaration

  1. Defined way

    1. Explicit typing:

      const B string = "hello"
    2. Implicit type definition: type inference using

      const B = "hello"
    3. Multi-constant declaration

      const(
          P1 = "hello"
          P2 = "golang"
      )
    4. If no initial value is a constant, consistent with the default constant value on line

  2. Enumerated type

    Use as set constants enumerated type, a set of data of the correlation value.

    const (
         SPRING = 1
         SUMMER = 2
         AUTUMN = 3
         WINTER = 4
     )
  3. Precautions

    1. Constant data type can only be: boolean, numeric (integer, floating point, complex numbers), character
    2. Constant never used, no error
    3. When explicitly specify the data type, you must ensure constant values ​​consistent with the type, the need to do an explicit conversion.

Guess you like

Origin www.cnblogs.com/henryno12/p/12324119.html