goroutine basis

Program 1:

 

main Package 

Import ( 
	"FMT" 
	"Time" 
) 

FUNC Test () { 
	var I int 
	for { 
		fmt.Println (I) 
		I ++ 
		time.sleep (time.Second) 
	} 
} 

// main process is a front 
FUNC main () { 
	/ / go open a coroutine 
	Go Test () 
}

  Results of the implementation of this program is empty, because the Lord out of the process.

Program 2:

main Package 

Import ( 
	"FMT" 
	"Time" 
) 

FUNC Test () { 
	I: = 0 
	for { 
		fmt.Println (I) 
		I ++ 
		time.sleep (time.Second) 
	} 
} 

FUNC main () { 
	Go Test () 
	Time. Sleep (time.Second * 10) // will print 10 digits, because the primary process lasted 10 seconds to exit 
}

  Procedure 3:

    

main Package 

Import ( 
    "FMT" 
    "Time" 
) 

FUNC Test () { 
    I: = 0 
    for { 
        fmt.Println (I) 
        I ++ 
        time.sleep (time.Second) 
    } 
} 

FUNC main () { 
    Go Test () 

    I: 100 = 
    for { 
        fmt.Println (I) 
        i-- 
        IF I == 90 { 
            BREAK 
        } 
        time.sleep (time.Second) 
    } 

    // will see two digital printing processes are alternately 
}

  

Guess you like

Origin www.cnblogs.com/zhangxiaoj/p/11258729.html
Recommended