tags:

views:

315

answers:

2

I'm trying to put a vector variable inside a struct in Google's Go programming language. This is what I have so far:

Want:

type Point struct { x, y int }
type myStruct struct {
 myVectorInsideStruct vector;
}

func main(){
 myMyStruct := myStruct{vector.New(0)};
 myPoint := Point{2,3};
 myMyStruct.myVectorInsideStruct.Push(myPoint);
}

Have:

type Point struct { x, y int }

func main(){
myVector := vector.New(0);
myPoint := Point{2,3};
myVector.Push(myPoint);
}

I can get the vector to work in my main function just fine, but I want to encapsulate it inside a struct for easier use.

+1  A: 

I'm not sure whether this is what you want, so leave a comment if it doesn't work:

package main

import "container/vector";

type Point struct { x, y int };

type mystruct struct {
    myVectorInsideStruct * vector.Vector;
}

func main() {
    var myMyStruct mystruct;
    myMyStruct.myVectorInsideStruct = new(vector.Vector);
    myPoint := Point{2,3};
    myMyStruct.myVectorInsideStruct.Push(myPoint);
}
Kinopiko
I think in place of `vector.New()` you now use `new(vector.Vector)`
Pat Notz
Yes, I noticed that: http://stackoverflow.com/questions/1806673/are-vector-assignments-copied-by-value-or-by-reference-in-googles-go-language/1806913#1806913
Kinopiko
+1  A: 

Not sure this is what you want, but:

package main

import (
    "fmt";
    "container/vector";
)

type myStruct (
    struct {
        myVectorInsideStruct vector.IntVector;
    }
)


func main() {
    v := new(myStruct);
    v.myVectorInsideStruct.Init(0);

    for i := 1 ; i < 10 ; i++ {
        v.myVectorInsideStruct.Push(i);
    }

    fmt.Printf("v.myVectorInsideStruct: %v\n", v.myVectorInsideStruct.Data());
}
RC
I did see the completed question (Point, ...) after answering
RC
It's OK to edit your answer if you want to.
Kinopiko
@Kinopino, your answer covers it :)
RC