Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create slice of unknown type?

Tags:

go

couchbase

I'm trying to fetch multiple values using this function (from go-couchbase).

func (b *Bucket) Gets(k string, rv interface{}, caso *uint64) error {
    data, _, cas, err := b.GetsRaw(k)
    if err != nil {
        return err
    }
    if caso != nil {
        *caso = cas
    }
    return json.Unmarshal(data, rv)
}

The problem is that I don't know how to create a slice of rv, since I don't know its type in my function, and I have no idea how big it will be so I can't really access the rv slice through indexes, right?

I want to be as general as possible, so creating a function per rv struct isn't really optimal and the GetBulk function only returns a []byte.

Is it even possible to do what I want in go?

like image 312
Filip Haglund Avatar asked Jul 16 '14 09:07

Filip Haglund


2 Answers

You can inspect the type information using the reflect package. If we have a function that has an argument rv interface{} where we expect a pointer to a slice to be passed in, we can create a reflect.Value that refers to that slice:

slice := reflect.ValueOf(rv).Elem()

We can reinitialise the slice as follows:

slice.Set(reflect.MakeSlice(slice.Type(), length, capacity))

We can determine the element type of this slice with:

elemType := slice.Type().Elem()

And then create another reflect.Value that represents a pointer to a newly allocated instance of that type:

v := reflect.New(elemType)

Now v.Interface() would be appropriate to pass to a function like json.Unmarshal. Once it has been filled, it can be appended to the slice:

slice.Set(reflect.Append(slice, v.Elem()))

For a full example that decodes a slice of JSON documents into an arbitrary slice type, see http://play.golang.org/p/G-SHQO2MAT

like image 130
James Henstridge Avatar answered Sep 30 '22 11:09

James Henstridge


Considering go-couchbase#Bucket.Gets takes an interface, you need to put your objects into a []interface, as describe in wiki page "InterfaceSlice"

var dataSlice []int = foo()
var interfaceSlice []interface{} = make([]interface{}, len(dataSlice))
for i, d := range dataSlice {
    interfaceSlice[i] = d
}

That means you already have a slice of "something", and know its size.

Or, since you want to "build a function around the Gets function", you could have the same signature, and use a simple interface{} instead of []interface.

like image 24
VonC Avatar answered Sep 30 '22 11:09

VonC