亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何并排打印鍵和值?

如何并排打印鍵和值?

Go
Helenr 2023-02-06 14:44:41
在此代碼中,我打印了角色,并且對于每個角色,我都想寫下附加到它的密鑰。但我不知道該怎么做。如果我i < 3在 for 循環中寫入,那么這三個鍵將被打印六次,因為該roles變量包含六個字符串值。package mainimport "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 _, data := range roles {        for i := 0; i < 1; i++ {            fmt.Println("Here is the "+keys[i]+":", data)        }    }}給出的結果Here is the name: first nameHere is the name: first emailHere is the name: first roleHere is the name: second nameHere is the name: second emailHere is the name: second role要求的結果Here is the name: first nameHere is the email address: first emailHere is the job role: first roleHere is the name: second nameHere is the email address: second emailHere is the job role: second role
查看完整描述

1 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

使用整數 mod 運算符將角色索引轉換為鍵索引:


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

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


// 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 && i > 0 {

        fmt.Println()

    }


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

}

在 playground 上運行示例



查看完整回答
反對 回復 2023-02-06
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號