go - 将CreationTimeStamp类型转换为字符串

标签 go kubernetes kubernetes-go-client

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

相关文章:

excel - 从 xls 文件中读取值但未正确读取数值

go - go语言中 `defer`怎么理解?

Kubernetes client-go : watch. Interface vs. cache.NewInformer vs. cache.NewSharedIndexInformer?

谷歌容器集群作为配置

go - 操作 Kubernetes 的 client-go PodList

linux - 如何监控网络接口(interface)(go-lang)

go - 使用 go build 没有收到警告消息

azure - 无法创建 Azure 容器服务类型 = Kubernetes

kubernetes - 如何在本地从谷歌云访问磁盘快照?

docker - Rancher置备节点将覆盖Docker镜像