指针问题

时间:2015-11-18 17:20:24

标签: pointers go append slice

TL; DR 不知何故,我在一个for循环的对象中追加一个指向列表而不是对象的指针,所以最后整个切片多次由同一个对象组成。我只是不知道如何解决这个问题。

漫长道路

我仍然很难找到指针。我昨天发布了一个问题并得到了一些帮助,但现在我在同一段代码中遇到了一个稍微不同的问题。

我正在使用gocqlcqlr go包来尝试为我的Cassandra查询找一个小的对象映射器。基本上我遇到的问题是我附加了一个似乎是指向对象的指针,而不是数组的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 Tweet
var s []Tweet
for 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 个答案:

答案 0 :(得分:0)

我至少可以告诉你你在做什么。 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)。简单来说,接口包含指向下面原始类型的指针(以及其他一些东西)。所以在你的函数中你传递一个指针(以及其他一些东西)。然后你附加指针。只需要具体并键入切换界面可能会很好。我假设你知道它可能是什么类型。在这种情况下,您必须具备以下内容:

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(),这样每次都会为你提供新的接口。每次追加后重新分配值。上一次通过循环会产生一个额外的..

相关问题