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

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

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

问题

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

期望的代码:

  1. type Point struct { x, y int }
  2. type myStruct struct {
  3. myVectorInsideStruct vector;
  4. }
  5. func main(){
  6. myMyStruct := myStruct{vector.New(0)};
  7. myPoint := Point{2,3};
  8. myMyStruct.myVectorInsideStruct.Push(myPoint);
  9. }

现有的代码:

  1. type Point struct { x, y int }
  2. func main(){
  3. myVector := vector.New(0);
  4. myPoint := Point{2,3};
  5. myVector.Push(myPoint);
  6. }

我可以在我的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:

  1. type Point struct { x, y int }
  2. type myStruct struct {
  3. myVectorInsideStruct vector;
  4. }
  5. func main(){
  6. myMyStruct := myStruct{vector.New(0)};
  7. myPoint := Point{2,3};
  8. myMyStruct.myVectorInsideStruct.Push(myPoint);
  9. }

Have:

  1. type Point struct { x, y int }
  2. func main(){
  3. myVector := vector.New(0);
  4. myPoint := Point{2,3};
  5. myVector.Push(myPoint);
  6. }

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

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

  1. package main
  2. import "container/vector";
  3. type Point struct { x, y int };
  4. type mystruct struct {
  5. myVectorInsideStruct * vector.Vector;
  6. }
  7. func main() {
  8. var myMyStruct mystruct;
  9. myMyStruct.myVectorInsideStruct = new(vector.Vector);
  10. myPoint := Point{2,3};
  11. myMyStruct.myVectorInsideStruct.Push(myPoint);
  12. }
英文:

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

  1. package main
  2. import "container/vector";
  3. type Point struct { x, y int };
  4. type mystruct struct {
  5. myVectorInsideStruct * vector.Vector;
  6. }
  7. func main() {
  8. var myMyStruct mystruct;
  9. myMyStruct.myVectorInsideStruct = new(vector.Vector);
  10. myPoint := Point{2,3};
  11. myMyStruct.myVectorInsideStruct.Push(myPoint);
  12. }

答案2

得分: 0

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

  1. package main
  2. import (
  3. "fmt";
  4. "container/vector";
  5. )
  6. type myStruct (
  7. struct {
  8. myVectorInsideStruct vector.IntVector;
  9. }
  10. )
  11. func main() {
  12. v := new(myStruct);
  13. v.myVectorInsideStruct.Init(0);
  14. for i := 1 ; i < 10 ; i++ {
  15. v.myVectorInsideStruct.Push(i);
  16. }
  17. fmt.Printf("v.myVectorInsideStruct: %v\n", v.myVectorInsideStruct.Data());
  18. }
英文:

Not sure this is what you want, but:

  1. package main
  2. import (
  3. &quot;fmt&quot;;
  4. &quot;container/vector&quot;;
  5. )
  6. type myStruct (
  7. struct {
  8. myVectorInsideStruct vector.IntVector;
  9. }
  10. )
  11. func main() {
  12. v := new(myStruct);
  13. v.myVectorInsideStruct.Init(0);
  14. for i := 1 ; i &lt; 10 ; i++ {
  15. v.myVectorInsideStruct.Push(i);
  16. }
  17. fmt.Printf(&quot;v.myVectorInsideStruct: %v\n&quot;, v.myVectorInsideStruct.Data());
  18. }

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:

确定