- Go語言高級編程(第2版)
- 柴樹杉 曹春暉
- 177字
- 2025-08-07 17:56:15
1.5.5 goroutine的創建
go
語句會在當前goroutine對應函數返回前創建新的goroutine。例如:
var a string func f() { print(a) } func hello() { a = "hello, world" go f() }
執行go f()
語句創建goroutine和hello()
函數是在同一個goroutine中執行,根據語句的書寫順序可以確定goroutine的創建發生在hello()
函數返回之前,但是新創建goroutine對應的f()
的執行事件和hello()
函數返回的事件則是不可排序的,也就是并發的。調用hello()
可能會在將來的某一時刻打印“hello, world”,也很可能是在hello()
函數執行完成后才打?印。
推薦閱讀
- JBoss Weld CDI for Java Platform
- Python快樂編程:人工智能深度學習基礎
- OpenCV實例精解
- Building a Home Security System with Raspberry Pi
- Banana Pi Cookbook
- Swift 4從零到精通iOS開發
- 新印象:解構UI界面設計
- Android應用開發實戰(第2版)
- Advanced Python Programming
- Java程序設計入門(第2版)
- Distributed Computing with Python
- 算法訓練營:海量圖解+競賽刷題(入門篇)
- Mastering PostgreSQL 11(Second Edition)
- Computer Vision with Python 3
- Vue.js從入門到精通