Learn about the Go language in this issue of Technology Weekly? , welcome to read~
文章推荐
"Handwritten Programming Language - Implementing Operator Overloading" by crossoverJie
Operator overloading is actually not a commonly used feature; because it changes the semantics of the operator, for example, it is obviously addition but is written as subtraction in the overloaded function.
This can make the code hard to read, but in some cases we would very much like the language itself to support operator overloading.
For example, decimal.Decimal, a third-party precision library commonly used in Go, can only use functions such as d1.Add(d2) when performing operations. When the operation is complex...
"Golang implements multi-storage-driven design SDK" by 7small7
Gocache is a multi-storage-driven cache extension component written in Go language. It brings you a lot of capabilities for caching data.
- Multiple cache-driven storage: support memory, redis or your custom storage driver. The following functions are supported:
- Chained caches: use multiple caches with a priority order (eg in-memory then fallback to redis shared cache).
- Loadable Cache: Allows you to call a callback function to put data back into the cache.
- Metrics cache, which allows you to store metrics about cache usage (hits, misses, set successes, set errors...).
- Automatically marshals/unmarshals cached values as a marshaller for structs.
- Define default values in storage and override them when setting data.
- Cache invalidation via expiration time and/or using tags.
- Use of generics.
"The randomness of Go clientSet Watch fails after running" by Jianjun
The List and Watch mechanism is one of the important mechanisms in kubernetes. The controller obtains all the latest versions of API objects through the API Server's List API, and monitors the changes of all API objects through the Watch API.
In the process of program design, it is often necessary to use the List && Watch mechanism to observe the status of the API object, so as to call the EventHandler and respond.
Based on this background, the official clientSet of the Go language provides corresponding API interfaces for developers to use. However, the author stepped on a lot of pits in using the Watch mechanism.
"Criticism of the Go2 Error Handling Proposal" by Fried Fish
By reviewing the design draft of Go2 error handling, we learned the usage and ideas of the check and handle functions. In response to the new grammar, new problems that may occur have been "criticized".
In general, the new syntax will increase the complexity and readability of the existing code on the disadvantage, can lead to all kinds of strange nesting, and will repeat with if err != nil, becoming a kind of New way of handling (one more).
Wouldn't it be as pure as if err != nil?
"The Pit of Variable Definition in Golang" by kumfo
First, the file structure is as follows:
. ├── a │ └── a.go ├── b │ └── b.go ├── go.mod └── main.go
"Several Tips to Improve the Efficiency of Go Language Development" by asong
The Go language allows a function to take any number of values as parameters. The Go language has a built-in ... operator, and the ... operator can only be used in the last parameter of the function. When using it, you must pay attention to the following:
Variable-length parameters must be at the end of the function list;
Parse the variable-length parameter as a slice. When the variable-length parameter has no value, it is a nil slice. The type of the variable-length parameter must be the same
"Using CGO in Go? These 7 questions you should pay attention to! " Author: Fried Fish
Cgo is an amazing technology that allows Go programs to interoperate with C libraries, which is a very useful feature.
Without it, Go wouldn't be where it is today. cgo is the key to running Go programs on Android and iOS.
I personally think cgo is overused in Go projects, and when faced with reimplementing a large chunk of C code in Go, programmers choose to use cgo to wrap the library, thinking it's an easier problem to solve. But I think this is a wrong choice behavior.
Obviously cgo is unavoidable in some cases, most notably you have to interoperate with graphics drivers or windowing systems, which are only available as binary blobs. In these scenarios, the use of cgo justifies its tradeoffs with far fewer tradeoffs than many are prepared to admit.
The following is an incomplete list of tradeoffs you may not be aware of when building your Go project on the cgo library.
问题推荐
Unanswered:
- go byte to string confusion?
- Golang development UDP multicast cannot receive messages on windows10?
- How to use quantlib in Goland?
- Use the pointer of go to convert the struct to each other and the byte is garbled. What is the reason for this and how to solve it?
Most frequent:
- How can golang import its own package?
- Go language, a redis storage json string problem?
- Golang foundation, why is the result of this program odd?
- How does the go language use errorgroup to notify the main coroutine when the sub-coroutine panics?
- How does go language implement coroutine waiting through channel or context?
- Does NewReader in go read all data?
# SegmentFault Technology Weekly#
"Technology Weekly" is a series of technical content specially launched by the community, with a weekly theme.
Update every Tuesday, welcome to " follow ". You can also leave a message on the topic you are interested in in the comments, and recommend excellent articles related to the topic.
If you have any questions, you can add Ms. WeChat~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。