1. Go function closures The Go language provides native support for closures. In Go, closures are function literals. The Go specification interprets closures in the following way.
function literals are closures: they can refer to variables defined in their wrapping function. These variables are then shared between the wrapping function and the function literals, and they continue to exist as long as they can be accessed.
Closures have a wide range of applications in Go, most often used in conjunction with the go keyword to create a new goroutine, such as the following code from the net/http package in the standard library.
Small company internal private Go module pulling solution
1. The cause of the problem With the introduction of the Go module in Go 1.11, the Go command to pull the dependent public go module is no longer a “pain point”. This is shown in the figure below.
We only need to configure a public GOPROXY service for the environment variable GOPROXY within our company/organization to easily pull all public go modules (public mods are open source mods).
Response design for gRPC services
1. Status of server-side response Developers doing back-end services are always sensitive to error handling, so they will always be very careful when doing the service response (response/reply) design.
If the back-end service is selected from HTTP API (rest api), such as json over http, the API response (Response) will mostly contain the following information.
1 2 3 4 5 6 7 { "code": 0, "msg": "ok", "payload" : { .
The first thing to do after switching to Go 1.18: replace all interfaces{} with any
With the release of Go 1.18, many Gopher’s can’t wait to download the release and experience the new features! After Go 1.18 arrives, what is the first thing you want to do? Speaking of which, many people will ask: What meme is this? This meme comes from Russ Cox on December 1, 2021 a commit to the Go language project. As you can see from the commit log, the main
How Go GC detects if a memory object contains a pointer
As we all know, Go is a programming language with garbage collection (GC), so developers usually don’t need to think about memory management, which reduces the mental burden, and while the Go program is running, the GC is quietly working behind the scenes to do the “aftercare “ for the developer: releasing the memory objects that can’t be reached periodically for subsequent reuse. GC only cares about pointer, as long
Talking about engineering practices for Go application output logging
Go is a back-end language that is known for developing various services, middleware and system platforms. When learning Go, logging is not indispensable or even unnecessary, but once we get to the real Go engineering practice, output logging is an inevitable problem we must face. Most of the services developed by Go have the property of continuous and autonomous operation, usually running 7×24 hours, and it is
Why does the "dependency hell" problem persist with the Go module?
If all Gophers abandoned the GOPATH build model and embraced the Go module build model, if all legacy Go package authors added go.mod to their legacy packages, and if all Go module authors adhered strictly to the semantic versioning (semver) specification, then Go would solve the “dependency hell” problem once and for all. But the reality is not so rosy! The “dependency hell problem” in Go still exists . In
Does Go support incremental builds?
The Go language is known in the programmer community for its fast compilation speed. This is due to the fact that Go chose to abandon the header file inclusion mechanism of its C ancestor in favor of package as the basic unit of compilation, which makes dependency analysis very simple and avoids the huge overhead of analyzing dependencies through header files as in C. Three specific reasons for Go’s fast
Is the result of the len(s) expression a constant or a variable?
len is a Go predefined identifier and also a Go built-in predefined function, through the go doc tool we can check the doc of the len function as follows. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $go doc builtin.len package builtin // import "builtin" func len(v Type) int The len built-in function returns the length of v, according to its type:
Asynchronous Programming Methods and Practices in JavaScript
1. Why Asynchronous Programming JavaScript is a single-threaded programming language that can only execute one task at a time. In order to handle different task scheduling logic, asynchronous programming is unavoidable in JavaScript development. The following scenarios necessarily involve asynchronous programming methods. IO operations: external device access File access TCP / UDP network access Asynchronous APIs setTimeout / setInterval setImmediate process.nextTick queueMicrotask Event 2. Several ways to implement asynchronous programming
Introduction to Linux TC Flow Control
Some time ago, when doing some tests, I came across Linux tc, because I needed to add a delay to the packets, I used netem in tc. It is very easy to add a simple delay, just one command like this: $ tc qdisc add dev eth0 root netem delay 1s, you don’t even need to fully understand the meaning of the parameters in the command . But when you want to do some more specific restrictions, (such as adding delay only to a specific ip port, or adding delay only to inbound traffic), things get a little tricky, and a simple Google search doesn’t seem to satisfy the requirements anymore.
SSL and GMVPN Handshake Protocol Explained
Previously, I wrote an article on SSL protocol, which introduces the fundamentals of cryptography and gives an overall overview of the SSL protocol. For space reasons, the detailed flow of the SSL handshake was not covered in depth. In this paper, we will disassemble the handshake process and introduce it in detail at the message level. If you don’t have the basic concepts of cryptography or don’t know the basic information of SSL protocol, it is recommended to read the previous article first.
Cryptography fundamentals and SSL/TLS protocols
SSL protocol is an important part of modern network communication, which provides data security at the transport layer. In order to facilitate your understanding, this article will start with the basics of cryptography, and then move on to a detailed explanation of the SSL protocol principles, processes and some important features, and finally will expand on the differences between the national SSL protocols, security and the key new features of TLS 1.
Function calling conventions in C
This article focuses on function calling conventions in C. The assembly code is combined with real-time observation of stack changes to visualize the process of function calls. This article only discusses the situation in x86/64 architecture, Linux/GCC environment, but other environments should be similar in overall idea and need to deal with these issues. Preface What is the Calling Convention? It is mainly to facilitate the sharing of code and
OpenSSL CVE-2022-0778 Vulnerability Recurrence and Illegal Certificate Construction
Vulnerability Description The vulnerability is in the BN_mod_sqrt() interface function, which is used to compute the square root of a modulus and expects that the argument p should be a prime number, but there is no check within the function, which leads to a possible infinite loop internally. This function is used when parsing certificates of the following format.
when the certificate contains an elliptic curve public key in compressed format certificate with explicit elliptic curve parameters whose base point is encoded in compressed format In short, this function is called when the certificate is parsed and the point coordinates need to be decompressed.
How the Linux kernel is booted
This article describes the boot process and the initialization process of the Linux kernel. What happens from the time you press power until you finally log in to the terminal? Note: This article is based on Linux 0.11, and the kernel source code can be downloaded from here. 0.11 is simple enough, but the basic functionality of the kernel is close enough to the current version to make it a
Nginx's shared memory management - the slab algorithm
I first learned about the slab algorithm when I was learning the Linux kernel. The kernel uses the Buddy System algorithm to manage memory pages. But for small objects, it would be wasteful to use a page allocator, so slab was born. The kernel’s kmalloc() is managed using slab. nginx’s shared memory management uses the same idea, but it is not as complex as slab in the Linux kernel. slab
In-depth understanding of mmap - kernel code analysis and driver demo examples
mmap is a very common system call in user space, whether it is allocating memory, reading and writing large files, linking dynamic library files, or sharing memory between multiple processes. This article first introduces the process address space and mmap, then analyzes the kernel code to understand its implementation, and finally deepens the understanding of mmap with a simple demo driver example. Process address space and vma As a pre-knowledge,
Should I use 777 or 0777 for os.Chmod?
Here’s the problem: I called os.Chmod("test.txt", 777) in the code, hoping to make the read/write and execute permissions of the file available to all users.
After executing the code, I used the command ls to look at the list of files by hand. Here is the list.
1 2 $ ls -l test.txt -r----x--x 1 cyhone 1085706827 0 Jun 20 13:27 test.txt Surprisingly, the file permissions do not change to rwxrwxrwx as expected.
Deep analysis of Golang sync.Pool underlying principles
sync.Pool is Golang’s built-in object pooling technology, which can be used to cache temporary objects to avoid the consumption and pressure on GC caused by frequent creation of temporary objects. You can see sync.Pool used extensively in many well-known open source libraries. For example, the HTTP framework Gin uses sync.Pool to reuse the gin.Context object that is created with each request. sync.Pool can also be found in grpc-Go, kubernates, and