在 go 編程中,函數和 goroutine 協同實現并發。goroutine 在函數中創建,函數的局部變量在 goroutine 中可見。goroutine 可以在實戰中用于并發處理任務,如并發文件上傳,通過創建負責上傳不同文件的 goroutine 提高效率。使用 goroutine 時需注意:創建 goroutine 需適量避免資源匱乏;goroutine 無返回值,獲取結果需使用并發原語;goroutine 無法直接停止或取消。
Go 函數與 Goroutine 的協同
在 Go 編程語言中,goroutine 是一種并發機制,可以創建輕量級線程來執行代碼。函數和 goroutine 相互配合,可以實現高效并發的編程。
函數與 Goroutine 的聯系
Goroutine 可以在函數內部創建,函數中的局部變量和常量在 goroutine 中可見。Goroutine 結束時,其局部變量和常量將被回收。
以下示例展示了如何在函數中創建 goroutine 并傳遞參數:
package main
import (
"fmt"
"time"
)
func printHello(name string) {
fmt.Printf("Hello, %s!\n", name)
}
func main() {
go printHello("World")
time.Sleep(1 * time.Second)
}
登錄后復制
在上述示例中,main 函數創建一個 goroutine 并傳入參數"World"。goroutine 執行 printHello 函數,打印出 "Hello, World!\n"。
實戰案例:并發文件上傳
考慮一個需要并發上傳多個文件的用例:
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"cloud.google.com/go/storage"
)
func uploadFile(w io.Writer, bucketName, objectName string) error {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
f, err := os.Open(objectName)
if err != nil {
return fmt.Errorf("os.Open: %v", err)
}
defer f.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
o := client.Bucket(bucketName).Object(objectName)
wc := o.NewWriter(ctx)
if _, err := io.Copy(wc, f); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("Writer.Close: %v", err)
}
fmt.Fprintf(w, "File %v uploaded to %v.\n", objectName, bucketName)
return nil
}
func main() {
bucketName := "bucket-name"
objectNames := []string{"file1.txt", "file2.txt", "file3.txt"}
for _, objectName := range objectNames {
go uploadFile(os.Stdout, bucketName, objectName)
}
}
登錄后復制
在這個案例中,main 函數創建一個 goroutine 列表,每個 goroutine 從操作系統中讀取一個文件并將其上傳到 Google Cloud Storage。這允許應用程序并發上傳多個文件,從而顯著提高性能。
注意事項
使用 goroutine 時需要注意以下事項:
Goroutine 是輕量級的,因此很容易創建大量 goroutine。確保不會創建過多的 goroutine 而導致程序資源匱乏。
Goroutine 退出時不帶任何返回值。如果要獲取 goroutine 的結果,請使用通道或其他并發性原語。
Goroutine 是匿名的,因此無法直接停止或取消單個 goroutine。






