string - Is there any way to show the output in pretty way without 7, -48 .... etc in print statements? -


the code given below

fmt.printf("%7s: %-48s\n", "iqn", annotations.iqn) fmt.printf("%7s: %-16s\n", "volume", args[0]) fmt.printf("%7s: %-15s\n", "portal", annotations.targetportal) fmt.printf("%7s: %-6s\n\n", "size", annotations.volsize) 

no, there not.

but can write utility function automates these, , need pass key-value pairs want pretty-print.

let's model key-values type:

type keyvalue struct {     key   string     value interface{} } 

note value may of type, not string value. we'll use default formatting when printing it. if want format other default, can convert string liking, , set value.

the pretty-print utility function (explanation follows below):

var aligns = map[bool]string{true: "-"}  func printkeyvalues(keyright, valueright bool, kvs ...keyvalue) {     // first convert values string , find max key , max value lengths:     values := make([]string, len(kvs))     maxkey, maxvalue := 0, 0     i, kv := range kvs {         if length := utf8.runecountinstring(kv.key); length > maxkey {             maxkey = length         }         values[i] = fmt.sprint(kv.value)         if length := utf8.runecountinstring(values[i]); length > maxvalue {             maxvalue = length         }     }      // generate format string:     fs := fmt.sprintf("%%%s%ds: %%%s%ds|\n",         aligns[keyright], maxkey+1, aligns[valueright], maxvalue+1)      // , print key-values:     i, kv := range kvs {         fmt.printf(fs, kv.key, values[i])     } } 

testing it:

printkeyvalues(false, true, []keyvalue{     {"iqn", "asdfl;kj"},     {"volume", "asdf;lkjasdf"},     {"portal", "asdf"},     {"size", 12345678}, }...) 

output:

    iqn: asdfl;kj     |  volume: asdf;lkjasdf |  portal: asdf         |    size: 12345678     | 

another test:

printkeyvalues(true, false, []keyvalue{     {"iqn", "asdfl;kj"},     {"volume", "asdf;lkjasdf"},     {"portal", "asdf"},     {"size", 12345678}, }...) 

output:

iqn    :      asdfl;kj| volume :  asdf;lkjasdf| portal :          asdf| size   :      12345678| 

try examples on go playground.

explanation

the printkeyvalues() first ranges on key-value pairs, , converts values string values, using default formatting calling fmt.sprint(). can find max key length , max value length. note length of string not len(s), returns byte-length in utf-8 encoding (which how go stores strings in memory). instead number of characters (or more precisely number of runes), used utf8.runecountinstring().

once have this, can generate format string max key length , max value length used. give possibility control whether want align keys , values left or right, in format string means - sign in case of right alignment. empty string "" in case of left , "-" in case of right, compact code used simple map:

var aligns = map[bool]string{true: "-"} 

indexing map false gives 0 value of value type of map "", , indexing true give associated value "-". , since map same, moved outside of function (small optimization).

to generate format string used fmt.sprintf():

fs := fmt.sprintf("%%%s%ds: %%%s%ds|\n",     aligns[keyright], maxkey+1, aligns[valueright], maxvalue+1) 

note % signs need doubled special in format strings.

one final task remained: use generated format string print key-value pairs.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -