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

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

type assertion of custom type in go using interface{}

问题

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

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

type Vertex struct{
	Adjacent DoubleLinkedList.DLinkedList
	Weight 	int 
	Origin 	int
}

type UGraph struct{
	Vertices map[int]DoubleLinkedList.DLinkedList
	Weight 	int 
	counter int 
	VerticesList []*Vertex
}

func (UG *UGraph) BFS(node *Vertex, Value interface{})(int) {

	visited := make(map[*Vertex]bool) // 作为集合的映射

	var queue Queue.Queue
	queue.Enqueue(node)

	for queue.Store.Size > 0 {
		V, _ := queue.Dequeue()
		nodes := UG.Vertices[((V).(Vertex).Origin).(int64)]
		
		head := nodes.Head
		for head.Next != nil {

			head = head.Next

			if head.Value == Value{
				return head.Value.Origin
			}

			if visited[(head.Value.Origin).(int64)] != true{
				visited[head.Value.Origin] = true
				queue.Enqueue(head)
			}
		}
	}
	return 0 
}

链接: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

type Vertex struct{
	Adjacent DoubleLinkedList.DLinkedList
	Weight 	int 
	Origin 	int
}

type UGraph struct{
	Vertices map[int]DoubleLinkedList.DLinkedList
	Weight 	int 
	counter int 
	VerticesList []*Vertex
}

func (UG *UGraph) BFS(node *Vertex, Value interface{})(int) {

	visited := make(map[*Vertex]bool) // map that acts as a set

	var queue Queue.Queue
	queue.Enqueue(node)

	for queue.Store.Size > 0 {
		V, _ := queue.Dequeue()
		nodes := UG.Vertices[((V).(Vertex).Origin).(int64)]
		
		head := nodes.Head
		for head.Next != nil {

			head = head.Next

			if head.Value == Value{
				return head.Value.Origin
			}

			if visited[(head.Value.Origin).(int64)] != true{
				visited[head.Value.Origin] = true
				queue.Enqueue(head)
			}
		}
	}
	return 0 
}

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

答案1

得分: 1

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

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

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

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

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

享受吧!


type DListNode[T any] struct {
	Prev  *DListNode[T]
	Next  *DListNode[T]
	Value T
}

func (DLL *DLinkedList[T]) AddFront(value T) { 

}

type Queue[T any] struct {
	Store    DLinkedList[T] // consider store a pointer 
	Capacity int
	Front    T
	Rear     T
}

func (QQ *Queue[T]) Enqueue(value T) error {

}

func (QQ *Queue[T]) Dequeue() (T, error) {
if QQ.isEmpty() {
        var zero T
		return zero, fmt.Errorf("Queue is currently Empty")
	}
    

}



// 省略一些步骤,你最终需要这个:
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


type DListNode[T any] struct {
	Prev  *DListNode[T]
	Next  *DListNode[T]
	Value T
}

func (DLL *DLinkedList[T]) AddFront(value T) { 

}

type Queue[T any] struct {
	Store    DLinkedList[T] // consider store a pointer 
	Capacity int
	Front    T
	Rear     T
}

func (QQ *Queue[T]) Enqueue(value T) error {

}

func (QQ *Queue[T]) Dequeue() (T, error) {
if QQ.isEmpty() {
        var zero T
		return zero, fmt.Errorf("Queue is currently Empty")
	}
    

}



// omitting some steps, you can see that you need this in the end:
func (UG *UGraph[T]) BFS(node *Vertex[T], value T) T {




</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:

确定