第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何并排打印鍵和值?

如何并排打印鍵和值?

Go
Helenr 2023-02-06 14:44:41
在此代碼中,我打印了角色,并且對于每個角色,我都想寫下附加到它的密鑰。但我不知道該怎么做。如果我i < 3在 for 循環(huán)中寫入,那么這三個鍵將被打印六次,因為該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)        }    }}給出的結(jié)果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要求的結(jié)果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條經(jīng)驗 獲得超5個贊

使用整數(shù) mod 運算符將角色索引轉(zhuǎn)換為鍵索引:


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 上運行示例。



查看完整回答
反對 回復(fù) 2023-02-06
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號