首頁  >  文章  >  後端開發  >  如何在 Golang 中列印 2 列表?

如何在 Golang 中列印 2 列表?

WBOY
WBOY轉載
2024-02-11 14:51:08962瀏覽

如何在 Golang 中打印 2 列表?

php小編百草帶您了解如何在Golang中列印2個清單。在Golang中,我們可以使用fmt套件中的Println函數來列印列表。首先,我們需要將兩個列表分別定義並初始化,然後使用Println函數將它們列印出來。透過使用循環和索引變量,我們可以逐一遍歷列表中的元素,並將它們列印出來。這樣,我們就能夠在Golang中輕鬆地列印出2個清單的內容。

問題內容

有點被這個問題困擾了。我的想法是有一個打印兩個列表的函數。第一個用於鍵,它具有固定的寬度。第二個是值,可能是很長的字串,其寬度取決於終端的當前寬度。

我想要的一個例子:

key1                                  value1value1value1value1
key2                                  value2value2value2value2value2value2value2value2value2value2value2
                                      value2value2value2value2value2value2value2value2value2value2value2
                                      value2value2value2value2value2value2

到目前為止,我取得的最佳成果是使用唇彩為第一列設定固定寬度。

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)
    }
}

其結果類似:

Key1                                  Value1Value1Value1Value1
Key2                                  Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2

那麼,如何按照我想要的方式格式化字串呢?我可以使用標準庫和外部庫,因此歡迎任何建議。

解決方法

我為此創建了一個函數。此函數有兩個參數,第一個用於列的映射變量,第二個參數用於每行填充多少個字元。它只是將鍵的值內容與空格更改為新變量,然後列印該鍵值。但如果您有未修改值的作品,則可以使用未修改變數。

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 < len(value); j += advance {
            
            // adjusting spaces of gap between key and value of next line
            if 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
}

這是一個範例輸出:

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

但請注意這一點,每次執行時鍵和值的順序可能不同,因為在帶有鍵、值對的 for 循環中列印時映射類型是無序的。

以上是如何在 Golang 中列印 2 列表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除