如何在Go语言中将一个向量放入结构体中?

huangapple go评论136阅读模式
英文:

How do I put a vector inside of a struct in Go?

问题

我正在尝试在Google的Go编程语言中将一个向量变量放入一个结构体中。目前我有以下代码:

期望的代码:

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);
}

现有的代码:

type Point struct { x, y int }

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

我可以在我的main函数中正常使用向量,但我希望将其封装在一个结构体中以便更容易使用。

英文:

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

得分: 2

我不确定这是否是您想要的,如果不起作用,请留下评论:

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);
}
英文:

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);
}

答案2

得分: 0

不确定这是否是您想要的,但是:

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());
}
英文:

Not sure this is what you want, but:

package main

import (
    &quot;fmt&quot;;
    &quot;container/vector&quot;;
)

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


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

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

    fmt.Printf(&quot;v.myVectorInsideStruct: %v\n&quot;, v.myVectorInsideStruct.Data());
}

huangapple
  • 本文由 发表于 2009年11月23日 11:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/1781028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定