Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert (untyped string constant) to *string [duplicate]

Tags:

string

go

persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
        ObjectMeta: metav1.ObjectMeta{
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec{
            StorageClassName: "manual",
            },
    }

StorageClassName parameter takes pointer to string, but compiler gives error when i'm passing string "manual" into it.

like image 323
manjeet Avatar asked Mar 06 '26 00:03

manjeet


2 Answers

You cannot get the address of a string constant/literal, but if you have a string local variable (set to the value you want) you can then pass the address of that local:

Declare a string local first and assign the constant string literal to it, then pass the address of that local as the parameter argument with the & operator:

persistentvolumeclaim := &apiv1.PersistentVolumeClaim {

        manualStr := "manual"

        ObjectMeta: metav1.ObjectMeta {
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec {
            StorageClassName: &manualStr,
        },
    }
like image 153
Dai Avatar answered Mar 08 '26 12:03

Dai


Use utils.StringPtr:

persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
        ObjectMeta: metav1.ObjectMeta{
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec{
            StorageClassName: utils.StringPtr("manual"),
            },
    }
like image 30
thwd Avatar answered Mar 08 '26 14:03

thwd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!