在Go语言中,使用接口类型进行自定义类型的类型断言。

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

type assertion of custom type in go using interface{}

问题

你好,我是你的中文翻译助手。以下是你要翻译的内容:

嗨,我是Go语言的新手,我正在尝试在Go中实现数据结构和算法作为学习过程。我试图将接口用作泛型值,但似乎无法使我的自定义顶点类型正常工作。我在下面附上了一个Go Playground的链接:

  1. type Vertex struct{
  2. Adjacent DoubleLinkedList.DLinkedList
  3. Weight int
  4. Origin int
  5. }
  6. type UGraph struct{
  7. Vertices map[int]DoubleLinkedList.DLinkedList
  8. Weight int
  9. counter int
  10. VerticesList []*Vertex
  11. }
  12. func (UG *UGraph) BFS(node *Vertex, Value interface{})(int) {
  13. visited := make(map[*Vertex]bool) // 作为集合的映射
  14. var queue Queue.Queue
  15. queue.Enqueue(node)
  16. for queue.Store.Size > 0 {
  17. V, _ := queue.Dequeue()
  18. nodes := UG.Vertices[((V).(Vertex).Origin).(int64)]
  19. head := nodes.Head
  20. for head.Next != nil {
  21. head = head.Next
  22. if head.Value == Value{
  23. return head.Value.Origin
  24. }
  25. if visited[(head.Value.Origin).(int64)] != true{
  26. visited[head.Value.Origin] = true
  27. queue.Enqueue(head)
  28. }
  29. }
  30. }
  31. return 0
  32. }

链接:https://go.dev/play/p/s3t9LCfBlRI

英文:

Hi Im new to go and I'm trying implement data structures and algos in go as a learning process. I am trying to interfaces as generics values but I cannot seem to get my custom type of vertex to function properly. I have attached a link to a go playground

  1. type Vertex struct{
  2. Adjacent DoubleLinkedList.DLinkedList
  3. Weight int
  4. Origin int
  5. }
  6. type UGraph struct{
  7. Vertices map[int]DoubleLinkedList.DLinkedList
  8. Weight int
  9. counter int
  10. VerticesList []*Vertex
  11. }
  12. func (UG *UGraph) BFS(node *Vertex, Value interface{})(int) {
  13. visited := make(map[*Vertex]bool) // map that acts as a set
  14. var queue Queue.Queue
  15. queue.Enqueue(node)
  16. for queue.Store.Size > 0 {
  17. V, _ := queue.Dequeue()
  18. nodes := UG.Vertices[((V).(Vertex).Origin).(int64)]
  19. head := nodes.Head
  20. for head.Next != nil {
  21. head = head.Next
  22. if head.Value == Value{
  23. return head.Value.Origin
  24. }
  25. if visited[(head.Value.Origin).(int64)] != true{
  26. visited[head.Value.Origin] = true
  27. queue.Enqueue(head)
  28. }
  29. }
  30. }
  31. return 0
  32. }

https://go.dev/play/p/s3t9LCfBlRI

答案1

得分: 1

如果我正确理解你的问题,这似乎是你的起点:

你需要一个支持泛型的队列。然后你需要更新所有的结构来处理一个像下面示例中的 T 泛型类型。

请注意,一旦你的结构体中有了类型 T,你的方法签名必须更新。

你不需要在每个类型上使用泛型。你可以使用一个已有类型的字段,比如 Queue[int]。

在这里,我们将考虑你希望所有都是泛型的。

享受吧!

  1. type DListNode[T any] struct {
  2. Prev *DListNode[T]
  3. Next *DListNode[T]
  4. Value T
  5. }
  6. func (DLL *DLinkedList[T]) AddFront(value T) {
  7. }
  8. type Queue[T any] struct {
  9. Store DLinkedList[T] // consider store a pointer
  10. Capacity int
  11. Front T
  12. Rear T
  13. }
  14. func (QQ *Queue[T]) Enqueue(value T) error {
  15. }
  16. func (QQ *Queue[T]) Dequeue() (T, error) {
  17. if QQ.isEmpty() {
  18. var zero T
  19. return zero, fmt.Errorf("Queue is currently Empty")
  20. }
  21. }
  22. // 省略一些步骤,你最终需要这个:
  23. func (UG *UGraph[T]) BFS(node *Vertex[T], value T) T {
英文:

If I understand correctly your issue, this seems to be your starting point:

You need a Queue with generics support. Then you need to update all structures to handle a T generic type like in the example below.

Be aware that once you have a type T in your struct, your method signature must be updated

You don’t need to use Generics on each type. You may want to use a field with an existing type like Queue[int]

Here we will consider you want all be generic

Enjoy

  1. type DListNode[T any] struct {
  2. Prev *DListNode[T]
  3. Next *DListNode[T]
  4. Value T
  5. }
  6. func (DLL *DLinkedList[T]) AddFront(value T) {
  7. }
  8. type Queue[T any] struct {
  9. Store DLinkedList[T] // consider store a pointer
  10. Capacity int
  11. Front T
  12. Rear T
  13. }
  14. func (QQ *Queue[T]) Enqueue(value T) error {
  15. }
  16. func (QQ *Queue[T]) Dequeue() (T, error) {
  17. if QQ.isEmpty() {
  18. var zero T
  19. return zero, fmt.Errorf("Queue is currently Empty")
  20. }
  21. }
  22. // omitting some steps, you can see that you need this in the end:
  23. func (UG *UGraph[T]) BFS(node *Vertex[T], value T) T {
  24. </details>

huangapple
  • 本文由 发表于 2022年3月27日 14:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71634101.html
匿名

发表评论

匿名网友

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

确定