卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35369本站已运行395

如何在 Golang 中打印 2 列表?

如何在 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 中跟踪第三个 Goroutine 中两个 Goroutine 的完成状态的最佳实践是什么?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏