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

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

如何在官方 neo4j go 驅(qū)動(dòng)程序中解析結(jié)果?

如何在官方 neo4j go 驅(qū)動(dòng)程序中解析結(jié)果?

Go
嗶嗶one 2023-05-15 14:41:45
當(dāng) Cypher 查詢?yōu)?MATCH 時(shí),我在解析neo4j-go-driver 官方驅(qū)動(dòng)程序的結(jié)果時(shí)遇到問題。使用 README.md 示例中的 CREATE 查詢可以正常工作,但使用 MATCH 不會(huì)對(duì)結(jié)果 Record().GetByIndex(0) 進(jìn)行索引result, err = session.Run("match(n) where n.id = 1 return n", map[string]interface{}{})if err != nil {    panic(err)}for result.Next() {    a := result.Record().GetByIndex(1)         //error: Index out or range    b := result.Record().GetByIndex(0).(int64) //error: interface {} is *neo4j.nodeValue, not int64    c := result.Record().GetByIndex(0) //prints corect result: &{14329224 [Item] map[id:1 name:Item 1]}    fmt.Println(c)}由于 nodeValue 不是導(dǎo)出類型,我不知道熱斷言屬性或整個(gè)接口返回到 nodeValue 類型。
查看完整描述

1 回答

?
互換的青春

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

您在查詢中指定的值return是從左到右索引的 0。因此,在您的示例中,由于您僅從MATCH(在本例中定義為n)返回一個(gè)值,因此它將在索引 0 處可用。如錯(cuò)誤消息所示,索引一超出范圍。


//in the following example a node has an id of type int64, name of type string, and value of float32


result, _ := session.Run(`

    match(n) where n.id = 1 return n.id, n.name, n.value`, nil)

                         // index 0 ^  idx 1^ . idx 2^


for result.Next() {

   a, ok := result.Record().GetByIndex(0).(int64)  //n.id

   // ok == true

   b, ok := result.Record().GetByIndex(0).(string) //n.name

   // ok == true

   c, ok := result.Record().GetByIndex(0).(float64)//n.value

   // ok == true

}

這可能是訪問節(jié)點(diǎn)上屬性值的慣用方式的基線——而不是嘗試訪問整個(gè)節(jié)點(diǎn)(驅(qū)動(dòng)程序通過將 nodeValue 保留為未導(dǎo)出的結(jié)構(gòu)隱式地阻止)從節(jié)點(diǎn)返回單個(gè)屬性,如上例所示。


與驅(qū)動(dòng)程序一起工作時(shí)需要考慮的其他幾點(diǎn)。Result還公開了一種Get(key string) (interface{}, ok)通過返回值的名稱訪問結(jié)果的方法。這樣,如果您需要更改結(jié)果的順序,您的值提取代碼將不會(huì)在嘗試訪問錯(cuò)誤索引時(shí)中斷。所以采取以上內(nèi)容并稍微修改一下:


result, _ := session.Run(`

        match(n) where n.id = 1 return n.id as nodeId, n.name as username, n.value as power`, nil)


for result.Next() {

    record := result.Record()

    nodeID, ok := record.Get("nodeId")

    // ok == true and nodeID is an interface that can be asserted to int

    username, ok := record.Get("username")

    // ok == true and username is an interface that can be asserted to string


}

最后要指出的是map[string]interface{}可用于將值作為參數(shù)傳遞給查詢。


session.Run("match(n) where n.id = $id return n", 

    map[string]interface{}{

      "id": 1237892

    })


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

添加回答

舉報(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)