我遵循关于 的常见模式go
tabwriter
其中相关编写器被初始化并用于将格式化输出打印到标准输出。
这是我的代码片段:
w := tabwriter.NewWriter(os.Stdout, 20, 30, 1, '\t', tabwriter.AlignRight)
for _, v := range listProjectsResponse.Projects {
parent := fmt.Sprintf("projects/%s/locations/-", v.ProjectId)
resp, err := containerService.Projects.Locations.Clusters.List(parent).Do()
if err != nil {
log.Fatalf(err.Error())
}
if len(resp.Clusters) > 0 {
fmt.Println("---------------------------------------------------------")
fmt.Printf("Project\t%s\n", v.ProjectId)
fmt.Println("---------------------------------------------------------")
fmt.Fprintf(w, "Cluster\t\tNode Count\n")
//w.Flush()
for _, p := range resp.Clusters {
fmt.Fprintf(w, "%s\t%d\t", p.Name, p.CurrentNodeCount)
//w.Flush()
}
w.Flush()
fmt.Printf("\n")
}
}
然而,似乎没有考虑列之间的公共距离,如以下输出所示:
Project my-project-1
---------------------------------------------------------
Cluster Node Count
my-cluster-1 3
---------------------------------------------------------
Project my-project-2
---------------------------------------------------------
Cluster Node Count
my-newest-project-1 2
---------------------------------------------------------
Project my-project-3
---------------------------------------------------------
Cluster Node Count
anothercluster 1
为什么每列不保持固定宽度(我认为根据相关编写器的初始化应该是 30 个字符长?)。上面的代码哪里配置错误了?
编辑:
我期望的是,每一列都有一个固定的,即第二列(节点数)始终出现在与中相同的位置(列)中
Project my-project-1
---------------------------------------------------------
Cluster Node Count
my-cluster-1 3
---------------------------------------------------------
Project my-project-2
---------------------------------------------------------
Cluster Node Count
my-newest-project-1 2
---------------------------------------------------------
Project my-project-3
---------------------------------------------------------
Cluster Node Count
anothercluster 1
犯罪嫌疑人X
相关分类