Go 语言编程 — 并发 — Goroutine 协程

目录

文章目录

go 语句

Golang 原生支持并发,体现在 Golang 提供了 go 关键字。

格式:

go 函数名(形参列表)

go 语句会直接开启一个新的运行时线程,即:Goroutine。Goroutine 的调度由 Go Runtime Scheduler 完成,同一个程序中所有的 Goroutine 共享同一个地址空间。

package main

import (
    "fmt"
    "time"
)

func say(s string) {
    for i := 0; i < 5; i++ {
        time.Sleep(100 * time.Millisecond)
        fmt.Println(s)
    }
}

func main() {
    go say("world")
    say("hello")
}

猜你喜欢

转载自blog.csdn.net/Jmilk/article/details/107164612