List of common structures/interfaces in 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C++ provides a rich set of open source libraries covering the following functions: data structures and algorithms (Standard Template Library) multi-threading, regular expressions (Boost) linear algebra (Eigen) graphical user interface (Qt) computer vision (OpenCV) machine learning (TensorFlow) Encryption (OpenSSL) Data compression (zlib) Network programming (libcurl) Database management (sqlite3)

The C++ standard library provides functions to handle DNS queries in network programming: gethostbyname(): Find host information based on the host name. gethostbyaddr(): Find host information based on IP address. dns_lookup(): Asynchronously resolves DNS.

There are 12 levels of Python exams, from beginner to advanced, in order to master Python's basic syntax, advanced features, advanced concepts and underlying mechanisms, etc., with gradually increasing difficulty.

Commonly used protocols in Java network programming include: TCP/IP: used for reliable data transmission and connection management. HTTP: used for web data transmission. HTTPS: A secure version of HTTP that uses encryption to transmit data. UDP: For fast but unstable data transfer. JDBC: used to interact with relational databases.

C++ functions can achieve network security in network programming. Methods include: 1. Using encryption algorithms (openssl) to encrypt communication; 2. Using digital signatures (cryptopp) to verify data integrity and sender identity; 3. Defending against cross-site scripting attacks ( htmlcxx) to filter and sanitize user input.

Java entry-to-practice guide: including introduction to basic syntax (variables, operators, control flow, objects, classes, methods, inheritance, polymorphism, encapsulation), core Java class libraries (exception handling, collections, generics, input/output streams , network programming, date and time API), practical cases (calculator application, including code examples).

Recently, the industry has generally paid great attention to the application of PHP software suites in Linux operating systems. As today's most popular server-side scripting language, PHP has a wide range of applications in the field of Web development. The Linux system has become the first choice for the majority of users due to its stable performance, high security and complete openness. This article aims to discuss in detail the actual application of the PHP software suite in the Linux system environment and its maximum integration effect. 1. Introduction to PHP suite The so-called PHP suite is essentially a comprehensive tool component that facilitates programmers to easily complete related program tasks, reduces the complexity of code development, and thereby improves development efficiency. Take Larv

The time it takes to master Golang varies from person to person, but it usually takes a few months to a few years. Learning stages include: Basic (1-2 months), Intermediate (3-6 months), Advanced (6-12 months or longer). Factors that accelerate learning include ongoing practice, project work, community involvement, and online resources. Influencing factors include prior programming experience, frequency of study, and study materials.
