【问题标题】:Convert CreationTimeStamp type to string将 CreationTimeStamp 类型转换为字符串
【发布时间】:2020-05-21 00:17:20
【问题描述】:

我从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
}

【问题讨论】:

    标签: go kubernetes kubernetes-go-client


    【解决方案1】:

    要将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 字段应该是切片(即 [] 字符串)而不是单个字符串。

    type json_format struct{
    Creation_Time []string
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多