php小編百草帶您了解在Golang中如何打印2個(gè)列表。在Golang中,我們可以使用fmt包中的Println函數(shù)來打印列表。首先,我們需要將兩個(gè)列表分別定義并初始化,然后使用Println函數(shù)將它們打印出來。通過使用循環(huán)和索引變量,我們可以逐個(gè)遍歷列表中的元素,并將它們打印出來。這樣,我們就能夠在Golang中輕松地打印出2個(gè)列表的內(nèi)容。
問題內(nèi)容
有點(diǎn)被這個(gè)問題困擾了。我的想法是有一個(gè)打印兩列表的函數(shù)。第一個(gè)用于鍵,它具有固定的寬度。第二個(gè)是值,可能是很長的字符串,其寬度取決于終端的當(dāng)前寬度。
我想要的一個(gè)例子:
key1 value1value1value1value1 key2 value2value2value2value2value2value2value2value2value2value2value2 value2value2value2value2value2value2value2value2value2value2value2 value2value2value2value2value2value2
登錄后復(fù)制
到目前為止,我取得的最好成果是使用唇彩為第一列設(shè)置固定寬度。
func printmetadata(metadata map[string]string, color string) { style := lipgloss.newstyle().width(32).foreground(lipgloss.color(color)) for k, v := range metadata { fmt.println(style.render(k) + v) } }
登錄后復(fù)制
其結(jié)果類似于:
Key1 Value1Value1Value1Value1 Key2 Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
登錄后復(fù)制
那么,如何按照我想要的方式格式化字符串呢?我可以使用標(biāo)準(zhǔn)庫和外部庫,因此歡迎任何建議。
解決方法
我為此創(chuàng)建了一個(gè)函數(shù)。該函數(shù)有兩個(gè)參數(shù),第一個(gè)用于列的映射變量,第二個(gè)參數(shù)用于每行填充多少個(gè)字符。它只是將鍵的值內(nèi)容與空格更改為新變量,然后打印該鍵值。但如果您有未修改值的作品,則可以使用未修改變量。
package main import ( "fmt" "errors" "strings" "sort" ) func main() { a := map[string]string{ "key1": strings.repeat("value1", 50), "key2": strings.repeat("value2", 50), "key3": strings.repeat("value3", 50), } err := columner(a, 30) if err != nil { fmt.println(err) } } func columner(m map[string]string, charamount int) error{ var keys []string var keylens []int // to avoid index panics and gathering keys for later usage for key, value := range m { if charamount > len(value) || charamount < 1{ return errors.new("error: charamount neither be greather than length of key's value nor below 1") } keys = append(keys, key) keylens = append(keylens, len(key)) } sort.ints(keylens) for i := 0; i < len(keys); i++ { // for storing updated value of key var value2 string value := m[keys[i]] // will used while extracting substring of key's value as first index firsti := 0 // last index for extract substring from key's value. the len of substring will be same as charamount charamount2 := charamount // will be used to advance next substring of key's value advance := charamount2 // spaces between between key and value // key value spacing := strings.repeat(" ", 20 + (keylens[0] - len(keys[i]))) // var for adjusting spaces of gap between key and value of next line // key value // value // to // key value // value spacingu := spacing + strings.repeat(" ", len(keys[i]) + 1) // this loop will be run as long as there is no substring left which exceed next line for j := 0; j 0 { spacing = spacingu } // add space between key and value, then extract substring, then add spaces to the next line of the // next substring of key's value value2 += spacing + value[firsti:charamount2] + "\n" // finish loop when there is no substring that can be exceed to next line if ((len(value) - charamount2) < advance) || ((len(value) - charamount2) == advance) { break } // changing first index to start index of next substring of key's value firsti = charamount2 // advancing to next substring of key's value charamount2 += advance } // add last remaining substring of key's value to variable which will be show as formatted. value2 += spacing + value[charamount2:] // show formatted key and value fmt.println(keys[i], value2, "\n") } return nil }
登錄后復(fù)制
這是一個(gè)示例輸出:
Key1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Value1Value1Value1Value1Value1 Key2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Value2Value2Value2Value2Value2 Key3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3 Value3Value3Value3Value3Value3
登錄后復(fù)制
但請注意這一點(diǎn),每次執(zhí)行時(shí)鍵和值的順序可能不同,因?yàn)樵趲в墟I、值對的 for 循環(huán)中打印時(shí)映射類型是無序的。