」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何在 Kubernetes PersistentVolumeClaim 中將非類型化字串傳遞給類型化指標?

如何在 Kubernetes PersistentVolumeClaim 中將非類型化字串傳遞給類型化指標?

發佈於2024-11-08
瀏覽:863

How to Pass an Untyped String to a Typed Pointer in Kubernetes PersistentVolumeClaim?

將非類型化字串傳遞給 Kubernetes PersistentVolumeClaim 中的類型化指標

當嘗試建立 Kubernetes PersistentVolumeClaim (PVName) 並指定 Storage參數時,開發人員可能會遇到錯誤,指出「無法將(無類型字串常數)轉換為*字串[重複]」。這是由於參數的預期指標類型與直接傳遞無類型字串常數的嘗試之間不匹配而引起的。

要解決此問題,必須先宣告一個字串局部變數並將無型別字串常數指派給它。隨後,應使用 & 運算子將字串局部變數的位址作為參數傳遞。

persistentvolumeclaim := &apiv1.PersistentVolumeClaim{

    // Declare a string variable and assign the untyped constant
    manualStr := "manual"

    ObjectMeta: metav1.ObjectMeta{
        Name: "mysql-pv-claim",
    },
    Spec: apiv1.PersistentVolumeClaimSpec{
        StorageClassName: &manualStr, // Pass the address of the string local variable
    },
}

透過遵循此方法,開發人員可以確保形參參數與預期的指標類型相匹配,從而解決轉換錯誤並允許 PVC 成功建立。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3