如何将键和值并排打印出来?

huangapple go评论78阅读模式
英文:

How to print the keys and values side by side?

问题

在这段代码中,我打印了角色,并且对于每个角色,我想要写入与之相关联的键。但是我不知道如何做到这一点。如果我在for循环中写入i < 3,那么这三个键会被打印六次,因为roles变量包含六个字符串值。

package main

import "fmt"

func main() {
    roles := []string{"first name", "first email", "first role", "second name", "second email", "second role"}
    keys := [3]string{"name", "email address", "job role"}
    for i := 0; i < len(roles); i += 3 {
        fmt.Println("Here is the "+keys[i/3]+":", roles[i])
        fmt.Println("Here is the "+keys[i/3+1]+":", roles[i+1])
        fmt.Println("Here is the "+keys[i/3+2]+":", roles[i+2])
    }
}

给定结果

Here is the name: first name
Here is the name: first email
Here is the name: first role
Here is the name: second name
Here is the name: second email
Here is the name: second role

所需结果

Here is the name: first name
Here is the email address: first email
Here is the job role: first role

Here is the name: second name
Here is the email address: second email
Here is the job role: second role
英文:

In this code, I print the roles, and with every role, I want to write the key attached to it. But I don't know how to do it. If I write i &lt; 3 in for loop then the three keys are printed six times because the roles variable contains six string values.

package main

import &quot;fmt&quot;

func main() {
	roles := []string{&quot;first name&quot;, &quot;first email&quot;, &quot;first role&quot;, &quot;second name&quot;, &quot;second email&quot;, &quot;second role&quot;}
	keys := [3]string{&quot;name&quot;, &quot;email address&quot;, &quot;job role&quot;}
	for _, data := range roles {
		for i := 0; i &lt; 1; i++ {
			fmt.Println(&quot;Here is the &quot;+keys[i]+&quot;:&quot;, data)
		}
	}
}

Given Result

Here is the name: first name
Here is the name: first email
Here is the name: first role
Here is the name: second name
Here is the name: second email
Here is the name: second role

Required Result

Here is the name: first name
Here is the email address: first email
Here is the job role: first role

Here is the name: second name
Here is the email address: second email
Here is the job role: second role

答案1

得分: 1

使用整数取模运算符将角色索引转换为键索引:

roles := []string{"first name", "first email", "first role", "second name", "second email", "second role"}
keys := []string{"name", "email address", "job role"}

// i 是 roles 的索引
for i := range roles {
    // j 是 keys 的索引
    j := i % len(keys)

    // 在分组之间打印空行。
    if j == 0 && i > 0 {
        fmt.Println()
    }

    fmt.Printf("这是 %s:%s\n", keys[j], roles[i])
}

在 playground 上运行示例

英文:

Use the integer mod operator to convert a roles index to a keys index:

roles := []string{&quot;first name&quot;, &quot;first email&quot;, &quot;first role&quot;, &quot;second name&quot;, &quot;second email&quot;, &quot;second role&quot;}
keys := []string{&quot;name&quot;, &quot;email address&quot;, &quot;job role&quot;}

// i is index into roles
for i := range roles {
	// j is index into keys
	j := i % len(keys)

	// Print blank line between groups.
	if j == 0 &amp;&amp; i &gt; 0 {
		fmt.Println()
	}

	fmt.Printf(&quot;Here is the %s: %s\n&quot;, keys[j], roles[i])
}

Run the example on the playground.

huangapple
  • 本文由 发表于 2022年8月31日 12:28:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73550878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定