目录
问题内容
解决方法
首页 后端开发 Golang 如何在 Golang 中打印 2 列表?

如何在 Golang 中打印 2 列表?

Feb 11, 2024 pm 02:51 PM
标准库

如何在 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

c++中绝对值怎么用 c++中绝对值怎么用 May 06, 2024 pm 06:15 PM

C++ 中获取绝对值的方法有两种:1. 使用内置函数 abs(),获取整型或浮点型的绝对值;2. 使用泛型函数 std::abs(),获取各类支持绝对值运算数据类型的绝对值。

c++中std::怎么用 c++中std::怎么用 May 09, 2024 am 03:45 AM

std 是 C++ 中包含标准库组件的命名空间。为了使用 std,需要使用 "using namespace std;" 语句。直接使用 std 命名空间中的符号可以简化代码,但建议仅在需要时使用,以避免命名空间污染。

python怎么弹出窗口 python怎么弹出窗口 May 05, 2024 pm 08:15 PM

在 Python 中弹出窗口可以使用两种方法:Tkinter:使用 Tkinter 库创建 Tk 或 TopLevel 窗口小部件。Pyglet:使用 Pyglet 库创建 Window 窗口。

c++中prime什么意思 c++中prime什么意思 May 07, 2024 pm 11:33 PM

prime 是 C++ 中的关键字,表示质数类型,只能被 1 和本身整除,用作布尔类型指示给定值是否为质数,为质数则为 true,否则为 false。

c++中fabs是什么意思 c++中fabs是什么意思 May 08, 2024 am 01:15 AM

fabs() 函数是 C++ 中的一个数学函数,用于计算浮点数的绝对值,去除负号并返回正值。它接受一个浮点参数,并返回一个 double 类型的绝对值。例如,fabs(-5.5) 将返回 5.5。该函数适用于浮点数,其精度受底层硬件影响。

config在java中什么意思 config在java中什么意思 May 07, 2024 am 02:39 AM

Config 在 Java 中表示配置信息,用于调整应用程序行为,通常存储在外部文件中或数据库中,可通过 Java Properties、PropertyResourceBundle、Java Configuration Framework 或第三方库进行管理,其好处包括解耦、灵活性、环境意识、可管理性、可扩展性。

_complex在c语言中的用法 _complex在c语言中的用法 May 08, 2024 pm 01:27 PM

complex 类型用于表示 C 语言中的复数,包含实部和虚部。其初始化形式为 complex_number = 3.14 + 2.71i,实部可通过 creal(complex_number) 访问,虚部可通过 cimag(complex_number) 访问。该类型支持常用的数学运算,如加、减、乘、除和取模。此外,还提供了一组用于处理复数的函数,如 cpow、csqrt、cexp 和 csin。

c++中min是什么意思 c++中min是什么意思 May 08, 2024 am 12:51 AM

C++ 中的 min 函数可返回多个值中的最小值。其语法为:min(a, b),其中 a 和 b 为要比较的值。还可以指定一个比较函数,以支持不支持 < 运算符的类型。C++20 引入了 std::clamp 函数,可处理三个或更多值的最小值。

See all articles