如何访问作为vector.Vector一部分的结构字段?

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

How do I access struct fields that are part of a vector.Vector?

问题

我正在寻求帮助,了解如何访问包含在container.vector.Vector中的结构字段。

以下是代码:

package main

import "fmt"
import "container/vector"

func main() {
    type Hdr struct {
        H string
    }
    type Blk struct {
        B string
    }

    a := new(vector.Vector)

    a.Push(Hdr{"Header_1"})
    a.Push(Blk{"Block_1"})

    for i := 0; i < a.Len(); i++ {
        fmt.Printf("a.At(%d) == %+v\n", i, a.At(i))
        x := a.At(i)
        fmt.Printf("%+v\n", x.H)
    }
}

产生错误prog.go:22: x.H undefined (type interface { } has no field or method H)

删除第21和22行产生:

a.At(0) == {H:Header_1}
a.At(1) == {B:Block_1}

那么,如何访问'H'或'B'?看起来我需要将这些接口转换为结构体,但是...我不知道。我很困惑。

感谢任何帮助。

英文:

I am looking for help understanding how to access struct fields that are inside a container.vector.Vector.

The following code:

package main

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

func main() {
	type Hdr struct {
        H string
    }
    type Blk struct {
    	B string
    }

    a := new(vector.Vector)

    a.Push(Hdr{&quot;Header_1&quot;})
    a.Push(Blk{&quot;Block_1&quot;})

    for i := 0; i &lt; a.Len(); i++ {
    	fmt.Printf(&quot;a.At(%d) == %+v\n&quot;, i, a.At(i))
    	x := a.At(i)
    	fmt.Printf(&quot;%+v\n&quot;, x.H)
    }
}

Produces the error prog.go:22: x.H undefined (type interface { } has no field or method H)

removing lines 21 and 22 produces:

a.At(0) == {H:Header_1}
a.At(1) == {B:Block_1}

So, how exactly does one access 'H' or 'B'? It seems like I need to convert those interfaces to structs, but... I dunno. I'm at a loss.

Thanks for any help.

答案1

得分: 4

使用Go的类型开关或类型断言来区分HdrBlk类型。例如,

package main

import (
	"fmt"
	"container/vector"
)

func main() {
	type Hdr struct {
		H string
	}
	type Blk struct {
		B string
	}

	a := new(vector.Vector)

	a.Push(Hdr{"Header_1"})
	a.Push(Blk{"Block_1"})

	for i := 0; i < a.Len(); i++ {
		fmt.Printf("a.At(%d) == %+v\n", i, a.At(i))
		x := a.At(i)
		switch x := x.(type) {
		case Hdr:
			fmt.Printf("%+v\n", x.H)
		case Blk:
			fmt.Printf("%+v\n", x.B)
		}
	}
}

然而,从weekly.2011-10-18版本开始:

> container/vector包已被删除。切片更好:SliceTricks

因此,对于最新版本,

package main

import "fmt"

func main() {
	type Hdr struct {
		H string
	}
	type Blk struct {
		B string
	}

	var a []interface{}

	a = append(a, Hdr{"Header_1"})
	a = append(a, Blk{"Block_1"})

	for i := 0; i < len(a); i++ {
		fmt.Printf("a[%d]) == %+v\n", i, a[i])
		x := a[i]
		switch x := x.(type) {
		case Hdr:
			fmt.Printf("%+v\n", x.H)
		case Blk:
			fmt.Printf("%+v\n", x.B)
		}
	}
}
英文:

Use a Go type switch or type assertion to distinguish between the Hdr and Blk types. For example,

package main

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

func main() {
	type Hdr struct {
		H string
	}
	type Blk struct {
		B string
	}

	a := new(vector.Vector)

	a.Push(Hdr{&quot;Header_1&quot;})
	a.Push(Blk{&quot;Block_1&quot;})

	for i := 0; i &lt; a.Len(); i++ {
		fmt.Printf(&quot;a.At(%d) == %+v\n&quot;, i, a.At(i))
		x := a.At(i)
		switch x := x.(type) {
		case Hdr:
			fmt.Printf(&quot;%+v\n&quot;, x.H)
		case Blk:
			fmt.Printf(&quot;%+v\n&quot;, x.B)
		}
	}
}

However, effective the weekly.2011-10-18 release:

> The container/vector package has been deleted. Slices are better:
> SliceTricks.

Therefore, for the latest releases,

package main

import &quot;fmt&quot;

func main() {
	type Hdr struct {
		H string
	}
	type Blk struct {
		B string
	}

	var a []interface{}

	a = append(a, Hdr{&quot;Header_1&quot;})
	a = append(a, Blk{&quot;Block_1&quot;})

	for i := 0; i &lt; len(a); i++ {
		fmt.Printf(&quot;a[%d]) == %+v\n&quot;, i, a[i])
		x := a[i]
		switch x := x.(type) {
		case Hdr:
			fmt.Printf(&quot;%+v\n&quot;, x.H)
		case Blk:
			fmt.Printf(&quot;%+v\n&quot;, x.B)
		}
	}
}

huangapple
  • 本文由 发表于 2012年1月17日 13:31:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/8890119.html
匿名

发表评论

匿名网友

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

确定