英文:
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 "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)
    }
}
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的类型开关或类型断言来区分Hdr和Blk类型。例如,
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 (
	"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)
		}
	}
}
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 "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)
		}
	}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论