-
Notifications
You must be signed in to change notification settings - Fork 0
golang
wangb edited this page Nov 2, 2018
·
1 revision
package main
import "fmt"
func main () {
fmt.Println("Hello World!")
}
- var a, b type = initial_a, initial_b
- a := initial_a (no var, can only in function)
- zero values // if value is not initialed the value will be zero value.
- const a = 111
- while:
for i < n
- c-style:
for i := 0; i < n; i++ {
- infinite-loop:
for {
- range iterate:
for i/k, v := range array/map {
- no ternary:
if i < 0 { a = 100 } else {a = 101}
- golang array has length in it's type
- var a [n]type
- literal value: [n]type{a, b, c}
- array without length
- make([]type, init_size)
- slice = append(slice, value)
- slice operator: slice[low:]
- make(map[key-type]val-type)
- n := map[string]int{"foo": 1, "bar": 2}
func funcname (a type) returntype
- multiple return type:
func div (a double, b double) (int, error)
- named return type
- pointer type:
*type
- dereference:
*pointer_variable
- variable to pointer:
&variable
type sname struct {
- struct method:
fun (r receiver_type) (a int) return_type
- values and pointers method are handled automatically in golang
type iface interface {
- empty interface:
interface {}
- any value match empty interface
- start a goroutine:
go fun()
make(chan string, buffer? int)
- channel directions:
- send: chan <- message
- receive: message := <- chan
- select: wait multiple channel
- timeout
- non-blocking
- close channel
- range over channel