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

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

指針問題。

指針問題。

Go
慕容森 2021-11-22 18:10:34
不知何故,我在對象的 for 循環中附加了一個指向列表而不是對象的指針,因此最后整個切片由同一對象多次組成。我只是不知道如何解決這個問題。漫漫長路我仍然很難在 go 中找出指針。我昨天發布了一個問題并得到了一些幫助,但現在我在同一段代碼中遇到了一個略有不同的問題。我有工作gocql,并cqlr去包,試圖咬一小對象映射為我的卡桑德拉查詢。本質上,我遇到的問題是我將似乎是指向對象的指針而不是 obj 的新實例附加到數組。我該如何解決?我試過在前面添加&和*,value但這似乎不起作用。我該如何解決這些問題?&根據他們的文檔,綁定函數需要一個。代碼type Query struct {    query       string    values      interface{}    attempts    int    maxAttempts int    structType  reflect.Type}func (query Query) RetryingQuery() (results []interface{}) {    var q *gocql.Query    if query.values != nil {        q = c.Session.Query(query.query, query.values)    } else {        q = c.Session.Query(query.query)    }    bindQuery := cqlr.BindQuery(q)    value := reflect.New(query.structType).Interface()    for bindQuery.Scan(value) {        fmt.Println(value)        results = append(results, value)    }    return}文檔要求var value type然后綁定你會通過&value。我引用了下面的文檔。var t Tweetvar s []Tweetfor b.Scan(&t) {    // Application specific code goes here    append(s, t)}問題是我不能直接var value query.structType定義它的類型然后將它的引用傳遞給bindQuery.Scan().印什么&{result1 x86_64 24 3.2.0-74-generic Linux}&{result2 x86_64 24 3.19.0-25-generic Linux}&{result3 x86_64 4 3.13.0-48-generic Linux}&{result4 x86_64 2 3.13.0-62-generic Linux}&{result5 x86_64 4 3.13.0-48-generic Linux}切片中有什么劇透,result5一遍又一遍地重復。我知道我只是將指向同一對象的指針附加到列表中,并且每次循環迭代對象都會更改,并且會將切片中的所有結果更改為該新對象。我只是不知道如何解決它。[{"hostname":"result5","machine":"x86_64","num_cpus":4,"release":"3.13.0-48-generic","sysname":"Linux"},{"hostname":"result5","machine":"x86_64","num_cpus":4,"release":"3.13.0-48-generic","sysname":"Linux"},{"hostname":"result5","machine":"x86_64","num_cpus":4,"release":"3.13.0-48-generic","sysname":"Linux"},{"hostname":"result5","machine":"x86_64","num_cpus":4,"release":"3.13.0-48-generic","sysname":"Linux"},{"hostname":"result5","machine":"x86_64","num_cpus":4,"release":"3.13.0-48-generic","sysname":"Linux"}]
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

好吧,我至少可以告訴你你在做什么。bindQuery需要一個指針。它更改存儲在地址中的值。


你基本上做的是這樣的:


package main


import "fmt"


func main() {

    var q int

    myInts := make([]*int, 0, 5)

    for i := 0; i < 5; i++ {

        q = i

        fmt.Printf("%d ", q)

        myInts = append(myInts, &q)

    }

    fmt.Printf("\n")

    for _, value := range myInts {

        fmt.Printf("%d ", *value)

    }

    fmt.Printf("\n")

    fmt.Println(myInts)

}

正如您可能猜到的那樣,它為您提供了以下信息:


0 1 2 3 4 

4 4 4 4 4 

[0x104382e0 0x104382e0 0x104382e0 0x104382e0 0x104382e0]

事情變得有點混亂reflect。您可以將您的類型作為接口,但僅此而已(除非您想使用unsafe)。簡單來說,接口包含一個指向底層原始類型(以及其他一些東西)的指針。所以在你的函數中你傳遞了一個指針(和其他一些東西)。然后你要附加指針。只是具體化并鍵入 switch 您的界面可能會很好。我假設你知道它可能是什么類型。在這種情況下,您必須按照以下方式進行操作:


package main


import (

    "fmt"

    "reflect"

)


type foo struct {

    fooval string

}


type bar struct {

    barval string

}


func main() {

    f1 := foo{"hi"}

    f2 := &foo{"hi"}

    b1 := bar{"bye"}

    b2 := &bar{"bye"}


    doSomething(f1)

    doSomething(f2)

    doSomething(b1)

    doSomething(b2)


}


func doSomething(i interface{}) {

    n := reflect.TypeOf(i)

    // get a new one

    newn := reflect.New(n).Interface()

    // find out what we got and handle each case

    switch t := newn.(type) {

    case **foo:

        *t = &foo{"hi!"}

        fmt.Printf("It was a **foo, here is the address %p and here is the value %v\n", *t, **t)

    case **bar:

        *t = &bar{"bye :("}

        fmt.Printf("It was a **bar, here is the address %p and here is the value %v\n", *t, **t)

    case *foo:

        t = &foo{"hey!"}

        fmt.Printf("It was a *foo, here is the address %p and here is the value %v\n", t, *t)

    case *bar:

        t = &bar{"ahh!"}

        fmt.Printf("It was a *bar, here is the address %p and here is the value %v\n", t, *t)

    default:

        panic("AHHHH")

    }

}

您也可以繼續value = reflect.New(query.structType).Interface()在循環內部調用,每次都會為您提供新的接口。每次追加后重新分配值。上次通過循環會使一個額外的雖然..


查看完整回答
反對 回復 2021-11-22
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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