A Tour of GO (1)--Basics

  1. Exported Names

A name is exported if it begins with capital letter.

  1. Multiple Returns
    func swap (x, y string) (string, string){...}
    a, b := swap("xxx", "yyy")
    
  2. Named Return Values
    func split(sum int) (x, y int) {
    ....
    return  //naked return
    }
    
    • Naked return should only be used in short functions to keep the readibility
  3. Variable with initializers can omit the type
    var i, j int = 1,2
    var a, b, c = true, false, "no!"
    
  4. Short variable declarations

    Can be used inside a function.
    func main(){
    k :=3
    ...
    }

  5. Basic Types:
    bool  //default false
    string  //default ""
    /* default value is 0 for numeric values */
    int int8 int16 int32 int64
    uint .... uint64 uintptr
    rune // alias for int32
    float32 float 54
    complex64 complex128
    
  • T(v) can convert v to type T.
  • ** items of different types need explicit conversions**. Such as int to float.
  1. Type interference.

    When declaring a variable without explicit type. the variable's type is inferred form the right side.
    var i int
    j := i // j is an int

    • The inference of numeric value depends on the precision
      i := 42 //int
      j := 3.142 //float64
      k := 0.867+0.51 //complex128
  2. Constants
    • Can not be declared with :=.
      const World="World"
    • Numeric Constant are high-precision values.

你可能感兴趣的:(A Tour of GO (1)--Basics)