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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用遞歸函數(shù)迭代遞歸結(jié)構(gòu)

使用遞歸函數(shù)迭代遞歸結(jié)構(gòu)

Go
胡子哥哥 2023-06-26 17:48:27
我有以下結(jié)構(gòu)    type Sitemap struct {        XMLName        xml.Name `xml:"urlset"`        Namespace      string   `xml:"xmlns,attr"`        Schema         string   `xml:"xmlns:xsi,attr"`        SchemaLocation string   `xml:"xsi:schemaLocation,attr"`        Root           *URLItem    }    type URLItem struct {        XMLName xml.Name `xml:"url"`        Loc     string   `xml:"loc"`        LastMod string   `xml:"lastmod,omitempty"`        Urls    []*URLItem    }    func (s *Sitemap) AddURL(key string, url string) {        node, found := findURLItemRecursive(s.Root, key)        if found {            node.Urls = append(node.Urls, &URLItem{Loc: url})        }    }    func findURLItemRecursive(urlItem *URLItem, key string) (*URLItem, bool) {        if urlItem.Loc == key {            return urlItem, true        }        for _, urlItem := range urlItem.Urls {            return findURLItemRecursive(urlItem, key)        }        return nil, false    }其中 是key父 URL,url是鏈接到父 URL 的子 URL,因?yàn)樽?URL 可以在父 URL 的頁(yè)面上找到。由于某些未知的原因findURLItemRecursive是有缺陷的。問(wèn)題是我無(wú)法UrlItem在第二級(jí)附加更多。我的意思是我可以創(chuàng)建該項(xiàng)目,為該項(xiàng)目Root創(chuàng)建切片,但是我無(wú)法創(chuàng)建嵌套切片。所以我不能超過(guò)第一層。UrlsRoot我想知道 Go 中的函數(shù)是否findURLItemRecursive有任何我無(wú)法發(fā)現(xiàn)的明顯錯(cuò)誤。
查看完整描述

1 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

我認(rèn)為這應(yīng)該對(duì)你有用。


    type Sitemap struct {

        XMLName        xml.Name `xml:"urlset"`

        Namespace      string   `xml:"xmlns,attr"`

        Schema         string   `xml:"xmlns:xsi,attr"`

        SchemaLocation string   `xml:"xsi:schemaLocation,attr"`

        Root           *URLItem

    }


    type URLItem struct {

        XMLName xml.Name `xml:"url"`

        Loc     string   `xml:"loc"`

        LastMod string   `xml:"lastmod,omitempty"`

        Urls    []*URLItem

    }


    func (s *Sitemap) AddURL(key string, url string) {

        node, found := findURLItemRecursive(s.Root, key)

        if found {

            node.Urls = append(node.Urls, &URLItem{Loc: url})

        }

    }



    func findURLItemRecursive(urlItem *URLItem, key string) (*URLItem, bool) {

        if urlItem.Loc == key {

            return urlItem, true

        }


        for _, urlItem := range urlItem.Urls {

            item, found := findURLItemRecursive(urlItem, key)

            if found {

                return item, found

            }

        }


        return nil, false

    }


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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