{"id":1252,"date":"2018-08-12T13:42:32","date_gmt":"2018-08-12T18:42:32","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1252"},"modified":"2018-08-12T13:42:32","modified_gmt":"2018-08-12T18:42:32","slug":"go-format-output-column-style","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/go-format-output-column-style\/","title":{"rendered":"Go Format Output Column Style"},"content":{"rendered":"

Similar to this article (using python) https:\/\/blog.ls-al.com\/python-output-align-by-column\/ I also did a quick Golang implementation. <\/p>\n

It is not quite as done as I would like but it is reasonably close. Similar to the python article the idea is to use something like an array to store column header descriptions and a value on how long the output strings should be and use it for header printing as well as line output statements.<\/p>\n

In addition I am doing some file operations here to create the output so the article has additional value.<\/p>\n

\r\npackage main\r\n\r\nimport (\r\n  "path\/filepath"\r\n  "os"\r\n  "flag"\r\n  "fmt"\r\n  "strconv"\r\n)\r\n\r\ntype rec struct {\r\n  fname, fsize string\r\n}\r\n\r\nvar i int64\r\nvar files map[int64]rec\r\nvar FORMAT map[string]int \r\n\r\nfunc PadLeft(str, pad string, length int) string {\r\n  for {\r\n   str = pad + str\r\n   if len(str) > length {\r\n     return str[0:length]\r\n   }\r\n  }\r\n}\r\n\r\nfunc printHeader(FORMAT map[string]int) {\r\n  for k, v := range FORMAT {\r\n    fmt.Printf("%[1]*[2]s ",v ,k)    \r\n  }\r\n  fmt.Println()\r\n  for k, v := range FORMAT {\r\n    _ = k\r\n    \/\/fmt.Printf("% [1]*[2]s ",v, "#")\r\n    fmt.Printf("%s   ",PadLeft("","#",v))    \r\n  }\r\n  fmt.Println()\r\n}\r\n\r\nfunc visit(path string, f os.FileInfo, err error) error {\r\n  fi, e := os.Stat(path)\r\n  if e != nil {\r\n    return e\r\n  }\r\n  i = i + 1\r\n  files[i] = rec{path, strconv.Itoa(int(fi.Size()))}\r\n  return nil\r\n} \r\n\r\nfunc main() {\r\n  FORMAT := map[string]int{"File Size": 10, "File Name": 11}\r\n  printHeader(FORMAT)\r\n  files = make(map[int64]rec)\r\n  flag.Parse()\r\n  root := flag.Arg(0)\r\n  err := filepath.Walk(root, visit)\r\n  _ = err\r\n  \/\/fmt.Printf("filepath.Walk() returned %v\\n", err)\r\n  for k, v := range files {\r\n    _ = k\r\n    fmt.Printf("%[1]*[2]s   %[3]*[4]s\\n", FORMAT["File Size"], v.fsize, FORMAT["File Name"], v.fname)\r\n  }\r\n}\r\n<\/pre>\n

Test Run<\/p>\n

\r\n$ go run header.go \/etc\/default\/\r\n File Size   File Name \r\n##########   ###########   \r\n       346   \/etc\/default\/acpid\r\n       290   \/etc\/default\/anacron\r\n       209   \/etc\/default\/saned\r\n       149   \/etc\/default\/apport\r\n       132   \/etc\/default\/speech-dispatcher\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Similar to this article (using python) https:\/\/blog.ls-al.com\/python-output-align-by-column\/ I also did a quick Golang implementation. It is not quite as done<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-1252","post","type-post","status-publish","format-standard","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1252"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}