如 什麼是適用於 Go 的 Azure SDK 一文所述,適用於 Go 的 Azure SDK 有一組管理和用戶端程式庫。 管理程式庫共用 Azure 身分識別支援、HTTP 管線和錯誤處理等功能。 如需管理程式庫的完整清單,請參閱 Azure SDK for Go 模組。
在本文中,您將瞭解使用管理程式庫與 Azure 資源互動的基本步驟。
安裝 Go 套件
在大部分專案中,您會安裝 Go 套件以進行版本設定和相依性管理。
若要安裝 Go 套件,請執行命令 go get 。
例如,若要安裝 armcompute 套件,請執行下列命令:
go get github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute
在大部分的 Go 應用程式中,您會安裝下列套件以進行驗證:
- github.com/Azure/azure-sdk-for-go/sdk/azcore/to
- github.com/Azure/azure-sdk-for-go/sdk/azidentity
將套件匯入 Go 程式代碼
下載之後,套件會透過下列 import 陳述式匯入您的應用程式:
import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
)
向 Azure 驗證
若要對 Azure 訂用帳戶執行程式代碼,您必須向 Azure 進行驗證。 azidentity 套件支援多個選項來向 Azure 進行驗證。 這些選項包括用戶端/秘密、憑證和受控識別。
默認驗證選項為 DefaultAzureCredential,其使用本文稍早設定的環境變數。 在 Go 程式代碼中,您會建立 azidentity 物件,如下所示:
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// handle error
}
建立資源管理用戶端
從 Azure 身分識別取得認證之後,請建立用戶端以連線到目標 Azure 服務。
例如,假設您想要連線到 Azure 計算 服務。 計算套件是由一或多個客戶端所組成。 用戶端會將一組相關的 API 分組,以提供指定訂用帳戶內其功能的存取權。 您可以建立一或多個用戶端來存取所需的 API。
以下程式碼使用 armcompute。NewVirtualMachinesClient 類型 來建立用戶端來管理虛擬機器:
client, err := armcompute.NewVirtualMachinesClient("<subscription ID>", cred, nil)
if err != nil {
// handle error
}
相同的模式是用來與其他 Azure 服務連線。 例如,安裝 armnetwork 套件,並建立 虛擬網路 用戶端來管理虛擬網路 (VNET) 資源。
client, err := armnetwork.NewVirtualNetworksClient("<subscription ID>", cred, nil)
if err != nil {
// handle error
}
程式代碼範例:
package main
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
)
func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
// handle error
}
client, err := armcompute.NewVirtualMachinesClient("<subscription ID>", cred, nil)
if err != nil {
// handle error
}
}
使用 Azure SDK for Go 參考文件
具現化用戶端之後,請使用它來對 Azure 資源進行 API 呼叫。 對於資源管理案例,大多數使用案例都是 CRUD (建立、讀取、更新、刪除) 作業。
若要尋找特定類型的作業,請遵循下列步驟:
- 移至適用於 Go 的 Azure SDK 參考檔。
- 搜尋套件。 (按 <Ctrl+F> 會自動展開頁面上的所有節點以進行搜尋。
- 選取套件。
- 在套件頁面上搜尋類型。
- 閱讀 Go 程式碼的類型描述和使用資訊。
您也可以將封裝的名稱附加至 github.com/Azure/azure-sdk-for-go/sdk/,以手動建置URL。
例如,如果您想要 compute/armcompute 參考文件,則 URL 是 github.com/Azure/azure-sdk-for-go/sdk/compute/armcompute。
此範例示範如何尋找 Azure 資源群組作業的參考檔:
- 移至 pkg.go.dev 上的主要 Azure SDK for Go 參考檔 。
- 選取 <Ctrl+F> ,然後輸入
resourcemanager/resources/armresources。 當您輸入搜尋字詞時,您會看到與 resources/armresources 套件非常接近的匹配。 - 為您的應用程式選取適當的套件。
- 閱讀「入門」部分或搜尋特定操作。 例如,搜尋 “resourcegroupsclient.create” (以建立資源群組) 會引導您前往 CreateOrUpdate 函式。
- 閱讀如何進行 API 呼叫以建立 Azure 資源群組。
長時間執行的作業
某些作業可能需要很長時間才能完成,因此管理程式庫具有透過非同步呼叫支援長時間執行作業 (LRO) 的函式。 這些函數名稱以 Begin、 like BeginCreate 和 BeginDelete開頭。
由於這些函式是非同步的,因此當您函式完成其工作時,您的程式碼不會封鎖。 相反地,函式會立即傳回 投票器 物件。 您的程式碼接著呼叫同步輪詢器函數,當原始的異步函數完成時,此函數會傳回。
下列代碼段顯示此模式的範例。
ctx := context.Background()
// Call an asynchronous function to create a client. The return value is a poller object.
poller, err := client.BeginCreate(ctx, "resource_identifier", "additional_parameter")
if err != nil {
// handle error...
}
// Call the poller object's PollUntilDone function that will block until the poller object
// has been updated to indicate the task has completed.
resp, err = poller.PollUntilDone(ctx, nil)
if err != nil {
// handle error...
}
// Print the fact that the LRO completed.
fmt.Printf("LRO done")
// Work with the response ("resp") object.
重點:
-
PollUntilDone函式需要一個輪詢間隔,指定它應該多長時間嘗試取得狀態。 - 間隔通常很短。 如需建議的時間間隔,請參閱特定 Azure 資源的文件。
- Go Azure SDK 設計指導方針頁面的 LRO 區段包含關於 LRO 的更進階的示例和一般指導方針。