-
Exported Names
A name is exported if it begins with capital letter.
-
Multiple Returns
func swap (x, y string) (string, string){...} a, b := swap("xxx", "yyy")
-
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
-
Variable with initializers can omit the type
var i, j int = 1,2 var a, b, c = true, false, "no!"
-
Short variable declarations
Can be used inside a function.
func main(){
k :=3
...
} -
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
tofloat
.
-
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
- The inference of numeric value depends on the precision
-
Constants
- Can not be declared with :=.
const World="World" - Numeric Constant are high-precision values.
- Can not be declared with :=.