Home >Backend Development >Golang >List of common structures/interfaces in Golang

List of common structures/interfaces in Golang

WBOY
WBOYforward
2024-02-13 17:30:11544browse

Golang 中的通用结构/接口列表

#php editor Baicao will introduce you to the list of common structures/interfaces in Golang in this article. Golang is an open source programming language that is easy to learn, efficient and reliable. It is widely used in network programming, cloud computing and other fields. In Golang, common structures and interfaces are very important concepts that can help us achieve code reusability and scalability. Through the introduction of this article, I believe readers can better understand and apply the common structures and interfaces in Golang and improve their programming skills.

Question content

Is there a way to get a list of common structures/interfaces in go?

This is what I want to achieve.

<code>package main

type List[T any] struct {
    Elements []T
}

func (f *List[T]) Add(el T) {
    f.Elements = append(f.Elements, el)
}

type ListInterface[T any] interface {
    Add(el T)
}

func main() {
    listOfLists := make([]ListInterface[any], 0)
    listOfLists = append(listOfLists, &List[int]{})
}
</code>

This is the error I get.

cannot use &List[int]{} (value of type *List[int]) as ListInterface[any] value in argument to append: *List[int] does not implement ListInterface[any] (wrong type for method Add)
    have Add(int)
    want Add(any)

So, if I understand correctly, in go any is its own type. It is not a synonym for "whatever type the runtime wants". My question is, is it possible to do something like this?

Workaround

What you are trying to do here suggests that you expect go's generics to be capable of type erasure (just like Java generics). but it is not the truth.

You have a List[int], which means its Add method looks like this:

func (l *List) Add(el int) {
    l.Elements = append(l.Elements, el)
}

Then try to add it to a slice of objects implementing that interface:

Add(v any)

Now, you might be thinking that int can be used as any, you are right, it can, but when you see:

var s []ListInterface[any]

You are saying that all elements in said slice will have an Add method that takes a parameter of type any, so this means:

s[0].Add("foo")
s[1].Add(123)

should always be a valid call. This does not hold if s[0] is of type List[int] (as is the case in your snippet). You are trying to append a string to Elements which is of type []int.

There is a saying that reverse engineering should be allowed:

s := []ListInterface[int]{}
s = append(s, &List[any]{})

It appears that List[any] will accept int parameters, but this is also not allowed. This can be useful in some cases, but in many cases this can be problematic.

Essentially, generics in Go are something that is handled at compile time. When you create a List[int], the compiler creates a type like List_int and implements the Add(el int) method on that type, Same as any other List type you end up using. None of these types will have an Add(any) method unless you create a List[any]. Think of it as compiler-assisted boilerplate code generation. Not runtime type erasure.

Result: List[int] and List[any] are completely different types and therefore cannot sit side by side in a slice as if they were the same type. If you want to be able to do what you want, you can do this:

func (l *List[T]) AddAny(v any) {
    tv, ok := v.(T)
    if !ok {
        return // or return an error
    }
    l.Add(tv)
}

Take the any approach of values, using a type assertion to see if a given value is compatible with the list's underlying type, and add it if that's the case. You can then add them into a single slice like this:

type Lists interface {
    AddAny(any)
}

s := []Lists{}
s = append(s, &List[int]{}, &List[string]{})
s[0].AddAny(123) // will work
s[0].AddAny("foo") // will not, with the current code this will silently fail
s[1].AddAny("foo") // works fine
s[1].AddAny(123) // silently fails

But in reality, when you do something like this, the code just screams X-Y problem, you are trying to use Y (generics) to solve your problem, when the real problem is X: what is the best way to solve it question?

The above is the detailed content of List of common structures/interfaces in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete