我从pod.CreationTimeStamp
获取时间并尝试将其存储在变量中。如何将时间存储在字符串中。
tmp := json_format{}
pods, _ := clientset.CoreV1().Pods(namespace).List(v1.ListOptions{LabelSelector:app_name})
for _, pod := range pods.Items {
tmp.Creation_Time = append(tmp.Creation_Time,pod.CreationTimestamp)
}
它给出此错误:
cannot convert pod.ObjectMeta.CreationTimestamp (type "k8s.io/apimachinery/pkg/apis/meta/v1".Time) to type string
type json_format struct{
Creation_Time string
}
最佳答案
要将CreationTimestamp
转换为字符串,可以使用String()
方法。
例:
timeInString := pod.CreationTimestamp.String()
您的代码:
tmp := json_format{}
pods, _ := clientset.CoreV1().Pods(namespace).List(v1.ListOptions{LabelSelector:app_name})
for _, pod := range pods.Items {
tmp.Creation_Time = append(tmp.Creation_Time,pod.CreationTimestamp.String())
}
另一个更正要求:
Creatio_Time
字段应为 slice (即[] string),而不是单个字符串。type json_format struct{
Creation_Time []string
}
关于go - 将CreationTimeStamp类型转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069771/