Chapter III go language

First, the basic function

  Definition: input, an output, to perform a specified task block

func function name (parameter list) (the returned list) { 
    code execution 
    return the returned list 
} 

// no arguments and return values 
func Test () { 
    fmt.Println ( "the helloWorld") 
} 
func main () { 
    Test () 
} 

// the return value of the multi function 
FUNC Test (x, y int, String Z) (int, String) { 
    // the same type of neighboring parameters x, y parameter type may be incorporated 
    // return worth multiple parentheses 
    n : = X + Y 
    return n-, Z 
} 

// return value named 
FUNC test2 (A, B int) (SUM int, int Sub) { 
    SUM = A + B 
    Sub = ab & 
    return 
} 
FUNC main () { 
    SUM, Sub : = test2 (100,800) 
    fmt.Println (SUM, Sub) 
} 

// variable parameters  
func test3 (b ... int) int {
    sum: = 0
    I for: = 0; I <len (B); I ++ { 
        SUM = SUM + B [I] 
    } 
    return SUM 
} 
FUNC main () { 
    SUM: = Test3 (10,20) can pass any parameters // 
    fmt. ( "SUM D =% \ n-"), SUM) printf 
}

Second, the function defer sentence

    Definition: There defer statement in a function, only to return when the execution will be executed when the order is more than defer from the back, and more resources for fat, use of resources defer closed

func test(){
    defer fmt.Println("Hello")
    fmt.Println("Parallel")
    fmt.Println("World")
)
//结果    Parallel  World  Hello

func test2(){
    defer fmt.Println("Hello")
    defer fmt.Println("Hello1")
    defer fmt.Println("Hello2")
    fmt.Println("Parallel")
    fmt.Println("World")
}
//结果    Parallel  World  Hello2  Hello1  Hello

fun test3(){
    for i:=0;i<5;i++{
        defer fmt.Println(i)
    }
}
//结果    4  3  2  1  0

func test4(){
    i := 0
    defer fmt.Println (i) 
    fmt.Println (i)
    i = 1000
}
// result when executing code 10000 to defer, parameters have been passed, but the final execution

Third, the built-in functions

    close: close to the main Channel (pipe)

    len: length for seeking (eg: string, array, slice, map, channel)

    new: to initialize int, struct, return pointer var a = new (int): to obtain a pointer of type int

    make: to initialize the channel, map, slice

    append: append element used to slice

    panic and recover: to do error handling  

  Variable scope and visibility

@ Scope: global variable, the function outermost 
var int = A 100 
FUNC Test () { 
    fmt.Printf ( "% D A = \ n-", A) 
} 

// Scope: local variables (1. Function 2. statement within the definition of the block definition) 
FUNC the Add (A int, int B) {int 
    var SUM int = 0 // local variable SUM 
    IF A> 0 { 
        var int = 100 C // C is a local variable, only if valid statement block 
    } 
} 

// visibility: any variable or function in the package are able to access and capitalize the first letter is exported, other packages can be accessed or call lowercase represents private and can not be used whine 
package Penalty for CAIC 
var int = 100 A 
var A = 200 is int 
FUNC the Add (A, B int) {int 
    return A + B 
} 
FUNC Sub (A, B int) {int 
    return ab & 
} 

Package CAIC 
FUNC the Test () {int 
    return A 
} // the same package name, you can call the
 
package main
Import ( "FMT", "CAIC") 
FUNC main () { 
    var 200 is S1 = int 
    var int S2 = 300 
    SUM: = calc.Add (S1, S2) 
    fmt.Printf ( "% D = S1 + S2", SUM ) // 500 
    fmt.Printf ( "% D = caic.A", caic.A) // 200 is 
    fmt.Printf ( "% D = caic.a", caic.Test ()) 
} // package can call caic the Add Test function and function well as a variable capital

Fourth, the anonymous function

// function is a type, it is possible to define a function of the type of a variable 
FUNC the Add (A, B int) {int 
    return A + B 
} 
FUNC testFunc1 () { 
    F1: = the Add 
    fmt.Printf ( "F1 =% of type T \ n ", f1) // print variables T% 
    SUM: = F1 (2,5) // function type variable 
    fmt.Printf (" SUM D =% \ n-", SUM) 
} 
FUNC main () { 
    testFunc1 () 
} 

// anonymous function, unnamed function 
FUNC testFunc2 () { 
    F1: FUNC = (a, B int) {// int anonymous function 
        return a + B 
    } 
    fmt.Printf ( "% T = type of F1 \ n-", F1) 
    SUM: = F1 (2,5) 
    fmt.Printf (" SUM D =% \ n-", SUM) 
} 
Funt main () { 
    testFunc2 () 
} 

// use the defer anonymous function 
func testFunc3 () {     
    defer func () {
        fmt.Printf("defer i=%d\n",i)    //100
    }()
    i = 100
    fmt.Printf("i=%d\n",i)
}

//函数作为一个参数
func add(a,b int) int{
    return a+b
}
func sub(a,b int) int{
    return a-b
}
func calc(a,b int,op func(int,int)int)int{
    return op(a,b)
}
func testDefer5(){
    sum := calc(100,300,add)
    sub := calc(100,300,sub)
    fmt.Printf("sum-%d sub-%d\n",sum,sub)
}

V. function closure

    Definition: a reference function and physical environment associated therewith a combination of (anonymous function nested, nested closure is a function of the external internal functions, reference variables inside a function of the external function, the function of external variables used up, the internal function reference and retained, used as a free variable, is the closure)

func Adder() func(int) int{
    var x int
    return func(d int) int{
        x += d
        return x
    }
}
func testClosure1(){
    f:= Adder()
    ret := f(1)
    fmt.Printf("ret=%d\n",ret)    //1
    ret = f(20)
    fmt.Printf("ret%d\n",ret)    //21
    ret = f(300)
    fmt.Printf("ret=%d\n",ret)    //321
}

//test
func add(base int) func(int) int{
    return func (i int) int{
        base += i
        return base
    }
}
func testClosure1(){
    tmp1 := add(10)
    fmt.Println(tmp1(1),tmp1(2))
    tmp2 := add(100)
    fmt.println(tmp2(1),tmp2(2))
}

//test2
func test(suffix string) func(string) string{
    return func(name string) string{
        if !strings.HasSuffix(name,suffix){
            return name + suffix
        }
        return name
    }
}
func testClosure1(){
    func1 := test(".bmp")
    func2 := test(".jpg")
    fmt.Println(func1("test"))    //test.bmp
    fmt.Println(func2("test"))    //test.jpg
}

//test3
func calc(base int) (func(int) int,func(int) int) {
    add := func(i int) int{
        base += i
        return base
    } 
    return add,sub
}
FUNC testClosure1 () { 
    F1, F2: Calc = (10) 
    fmt.Println (F1 (. 1), F2 (2)). 11. 9 // 
    fmt.Println (F1 (. 3), F2 (. 4)) // 12 is . 8 
    fmt.Println (F1 (. 5), F2 (. 6)). 7 // 13 is 
    fmt.Println (F1 (. 7), F2 (. 8)). 6 // 14 
} 

// Test4 
FUNC testClosure1 () { 
    for I: = 0; i <. 5; i ++ { 
        Go FUNC () { 
            Fmt.Println (i) 
        } () 
    } 
    time.sleep (time.Second) 
} 
// output pit 55555, i is assigned to each function, but the 5 when printing 

// Test5 
FUNC testClosure1 () { 
    for I: = 0; I <5; I ++ { 
        Go FUNC (int index) { 
            fmt.Println (index) 
        } (I) 
    }
    time.sleep (time.Second) 
} 
// Output 01234

  

  

Guess you like

Origin www.cnblogs.com/parallel-Y/p/11412602.html