php editor Xinyi introduces to you the bag types in golang. In golang, the bag type refers to a data structure used to store a collection of elements. Unlike other data structures, elements in a bag can appear repeatedly and the order does not matter. This means that the bag type is very useful in situations where order is not required, such as calculating the frequency of elements or determining whether an element exists. It should be noted that the bag type and string type do not match in golang, so you need to pay attention to type conversion issues during use.
Question content
Don’t ask me why I do this, just tell me how it is possible:
gopls error: string and string type mismatch
type mapsi2[t string | int | float32 | float64] struct { keys []string values []t } func (mapsi mapsi2[string]) setvalue(key string, value string) { for i, keymapsi := range mapsi.keys { if key == keymapsi { mapsi.values[i] = value } } }
At first I thought the lsp server was stupid, but it turned out not to be the case.
Error occurred: string and string type mismatch
go run ./cmd/app # devllart/foobarman/src/mapsi src/mapsi/mapsi.go:48:13: invalid operation: key == keyMapsi (mismatched types string and string) make: *** [Makefile:6: run] Error 2
I googled and in the search results I get an error only when comparing pointers to strings... right there, the type everything is fine, or I'm wrong.
Workaround
Your method signature should be func (mapsi Mapsi2[T]) SetValue(key string, value T)
.
has nothing to do with your compilation issue, but please note:
- You may want to use a pointer receiver so that changes persist beyond method calls
- You may also want to handle the case where the key is not found
View on the playground: https://www.php.cn/link/6934456f54af5ab56c6f347c6427afeb一个>.
The above is the detailed content of Maybe it's bag in golang (type string and string don't match). For more information, please follow other related articles on the PHP Chinese website!

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools