搜尋
首頁後端開發Golang如何在 Go 模板中同時迭代多個陣列?

How to Iterate Over Multiple Arrays Simultaneously in Go Templates?

Go 範本:同時迭代多個陣列

在開發專案時,您可能會遇到需要同時迭代多個陣列的情況去模板。當您具有如下所示的資料結構並要求將這些數組中的每個元素分組到輸出中時,就會發生這種情況。

type Schedule struct {
    Description   string
    ControlNights int
    PlayNights    int
    StartDay      int
    Combos        []Combo
}

type Combo struct {
    From    time.Time
    Every   int
    Until   time.Time
    Sounds  []string
    Volumes []int
    Waits   []int
}

迭代組合

要單獨顯示每個組合的數據,您可以使用範圍循環迭代 Schedule.Combos 陣列。這將迭代每個組合。但是,這不足以顯示您所需的數據。

將數組組合成行

將不同數組中的資料分組到行中,其中每行包含來自聲音的資料、捲和等待數組,您可以使用動態模板。此技術可讓您操作和修改範本本身內的資料。

用於分組的動態模板

考慮以下模板:

{{ range .Schedule.Combos }}
<div class="container">
    <div class="row">
        <div class="col">
            <div class="card mt-2 ml-2">
                <div class="card-body">
                    <h5 id="strong-Timing-strong"><strong>Timing</strong></h5>
                    <h5 id="Play-every-Every-mins">Play every {{.Every}} mins</h5>
                    <h5 id="From-time-TimeOfDayToString-From">From time {{TimeOfDayToString .From}}</h5>
                    <h5 id="Until-TimeOfDayToString-Until">Until {{TimeOfDayToString .Until}}</h5>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card mt-2">
                <div class="card-body">
                    <h5 id="strong-Sounds-strong"><strong>Sounds</strong></h5>
                    {{ range .Sounds }}
                    <h5 id="Sound">Sound {{.}}</h5>
                    {{ end }}
                </div>
            </div>
        </div>
<p>要將聲音、音量和等待分組在一起,您可以建立一個新的模板變數來組合所有三個陣列。 </p>
<pre class="brush:php;toolbar:false">{{ $mergedArrays := .Sounds, .Volumes, .Waits }}

迭代組合數組

使用可用的組合數組,您可以使用嵌套循環迭代它們:

{{ range $index, $mergedArray := $mergedArrays }}
    {{ $sound := $mergedArray[0] }}
    {{ $volume := $mergedArray[1] }}
    {{ $wait := $mergedArray[2] }}
    
  • {{ $sound }} - {{ $volume }} - {{ $wait }}
  • {{ end }}

    在此巢狀循環中,您存取組合數組的各個元素並顯示它們

    這種方法可讓您將聲音、音量和等待數組中的資料分組到所需的行中。

    以上是如何在 Go 模板中同時迭代多個陣列?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    GO中的字符串操縱:掌握'字符串”軟件包GO中的字符串操縱:掌握'字符串”軟件包May 14, 2025 am 12:19 AM

    掌握Go語言中的strings包可以提高文本處理能力和開發效率。 1)使用Contains函數檢查子字符串,2)用Index函數查找子字符串位置,3)Join函數高效拼接字符串切片,4)Replace函數替換子字符串。注意避免常見錯誤,如未檢查空字符串和大字符串操作性能問題。

    去'字符串”包裝提示和技巧去'字符串”包裝提示和技巧May 14, 2025 am 12:18 AM

    你應該關心Go語言中的strings包,因為它能簡化字符串操作,使代碼更清晰高效。 1)使用strings.Join高效拼接字符串;2)用strings.Fields按空白符分割字符串;3)通過strings.Index和strings.LastIndex查找子串位置;4)用strings.ReplaceAll進行字符串替換;5)利用strings.Builder進行高效字符串拼接;6)始終驗證輸入以避免意外結果。

    GO中的'字符串”軟件包:您的首選字符串操作GO中的'字符串”軟件包:您的首選字符串操作May 14, 2025 am 12:17 AM

    thestringspackageingoisesential forefficientstringManipulation.1)itoffersSimpleyetpoperfulfunctionsFortaskSlikeCheckingSslingSubstringsStringStringsStringsandStringsN.2)ithandhishiCodeDewell,withFunctionsLikestrings.fieldsfieldsfieldsfordsforeflikester.fieldsfordsforwhitespace-fieldsforwhitespace-separatedvalues.3)3)

    Go Bytes軟件包與字符串軟件包:我應該使用哪個?Go Bytes軟件包與字符串軟件包:我應該使用哪個?May 14, 2025 am 12:12 AM

    WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

    如何使用'字符串”軟件包逐步操縱字符串如何使用'字符串”軟件包逐步操縱字符串May 13, 2025 am 12:12 AM

    Go的strings包提供了多種字符串操作功能。 1)使用strings.Contains檢查子字符串。 2)用strings.Split將字符串分割成子字符串切片。 3)通過strings.Join合併字符串。 4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。 5)用strings.ReplaceAll替換所有指定子字符串。 6)使用strings.HasPrefix或strings.HasSuffix檢查字符串的前綴或後綴。

    Go Strings軟件包:如何改進我的代碼?Go Strings軟件包:如何改進我的代碼?May 13, 2025 am 12:10 AM

    使用Go語言的strings包可以提升代碼質量。 1)使用strings.Join()優雅地連接字符串數組,避免性能開銷。 2)結合strings.Split()和strings.Contains()處理文本,注意大小寫敏感問題。 3)避免濫用strings.Replace(),考慮使用正則表達式進行大量替換。 4)使用strings.Builder提高頻繁拼接字符串的性能。

    GO BYTES軟件包中最有用的功能是什麼?GO BYTES軟件包中最有用的功能是什麼?May 13, 2025 am 12:09 AM

    Go的bytes包提供了多種實用的函數來處理字節切片。 1.bytes.Contains用於檢查字節切片是否包含特定序列。 2.bytes.Split用於將字節切片分割成smallerpieces。 3.bytes.Join用於將多個字節切片連接成一個。 4.bytes.TrimSpace用於去除字節切片的前後空白。 5.bytes.Equal用於比較兩個字節切片是否相等。 6.bytes.Index用於查找子切片在largerslice中的起始索引。

    使用GO的'編碼/二進制”軟件包掌握二進制數據處理:綜合指南使用GO的'編碼/二進制”軟件包掌握二進制數據處理:綜合指南May 13, 2025 am 12:07 AM

    theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,確保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,寫下,寫,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary

    See all articles

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅動的應用程序,用於創建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱門文章

    熱工具

    SublimeText3 英文版

    SublimeText3 英文版

    推薦:為Win版本,支援程式碼提示!

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )專業的PHP整合開發工具

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    將Eclipse與SAP NetWeaver應用伺服器整合。

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

    WebStorm Mac版

    WebStorm Mac版

    好用的JavaScript開發工具