go_study/bilibili/aceld/goroutine/test1/main.go

30 lines
551 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"time"
)
func newTask() {
i := 0
for {
i++
fmt.Printf("new Goroutin: i = %d\n", i)
time.Sleep(1 * time.Second)
}
}
func main() {
// 创建一个go程去执行newTask()流程
go newTask()
// 如果直接打印一句话并退出,那么整个进程都结束了,go程也就随着结束了
fmt.Println("main goroutine exit")
// 如果进程继续运行,那么go程才可以进行执行
// i := 0
// for {
// i++
// fmt.Printf("main goroutio: i = %d\n", i)
// time.Sleep(1 * time.Second)
// }
}