In Go 1.21, three functions related to sync.Once have been added. sync.Once itself is very simple to implement, but what do these three new functions do? Let’s take a look.
sync.Once We often use sync.Once to implement the singleton pattern, which is also very efficient.
The code below is an official example, run it and you can see that the onceBody function will only be executed once.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package main import ( "fmt" "sync" ) func main() { var once sync.
Initialize Gcp Environment Using Terraform
We will be using Terraform to initialize the GCP environment. This article will take you step by step from creating a Service Account to GCS and saving Terraform State for version control.
Preparation Installing Terraform Please refer to the Official Terraform Documentation to install Terraform. MacOS can be installed using Homebrew.
1 brew install terraform Installing Google Cloud SDK Please refer to the Google Cloud SDK official documentation to install Google Cloud SDK.
Three new built-in functions added in Go 1.21
There are three new built-in functions in Go 1.21. Compared to the previous functions len, cap, delete, append, close, panic and so on, there are new changes, let’s take a look at them.
clear clear has always been a function that everyone wanted, and this new function has been added to manipulate map and slice objects.
1 func clear[T ~[]Type | ~map[Type]Type1](t T) For map object: clear function clears all the elements of the map object For the slice object: the clear function sets all elements to the zero value of the element type, with the same length and the same capacity Write a program to test that it is indeed as described above:
List all Pods under Service in Kubernetes
In Kubernetes (k8s for short), a Service Endpoint is an abstraction that represents the network location of a service. It is an internal or external IP address that can be used to access the application services running in a Kubernetes cluster. How do you list all the Pods under a Service in Kubernetes? This article will introduce how to list all Pods under a Service using kubectl.
Prepare the environment Before we start, we need to prepare the k8s environment.
Determine if the User-Agent is forged
User-Agent uses a specific string to identify web client information, and User-Agent is often used by websites for platform judgment, crawler detection, etc. This article introduces some general methods to determine whether a User-Agent is forged or not.
1. Format of User-Agent User-Agent is a standard Header field when a web client initiates an HTTP request, which uses a specific string to identify web client information. The User-Agent is so important that it is often used by websites for platform determination, crawler detection, and so on.
What can be summarized from 100 Go Mistakes?
This article is mainly to summarize and refine the content of “100 Go Mistakes How to Avoid Them”, both veterans and novices can actually read it, many problems are easy to be ignored. The author of this book is also very difficult, taking into account so many problems that arise when using Go. Note the shadow variable 1 2 3 4 5 6 7 8 9 10 11 12 13
How do I block a Go program from exiting?
I’d like to share a collection of ways to block Go programs from exiting, and there are still some brainy ways to do it.
In a program like the one below, the program exits as soon as it runs. The reason is that the main goroutine is finished executing, and the child goroutine exists, but it fails to block the main goroutine’s execution.
1 2 3 4 5 package main import "net/http" func main() { go http.
Read and write network packets in batches for high performance - Addendum
The previous article introduced the sendmmsg system call, which sends network packets in batches, and this article talks about how this differs from writeev.
In fact, looking at their definitions, you can see where the difference lies.
1 2 int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags); ssize_t writev(int fd, const struct iovec *iov, int iovcnt); The sendmmsg() system call is an extension of sendmsg(2) that allows the caller to transmit multiple messages on a socket using a single system call.
Read and write network packets in batches for high performance
Although the network protocol stack provides a wealth of features that allow us to easily achieve data exchange on the network, but sometimes we are not so satisfied with the performance of the stack, the previous articles I also introduced the way to efficiently process network data through XDP and other technologies, but after all, XDP is not yet so widely used, and it is not yet so simple to use.
Writing MySQL UDF with Golang
I. MySQL UDF This thing is called “MySQL user-definable function”, so what exactly does UDF do?
In a nutshell, it means: you can write some code to handle the data, then compile the code into a dynamic link library (so), and finally load it dynamically in MySQL and then the user can use it.
II. Solution Since we have to check the database, but actually the review does not focus on each table or even the database details; so the simplest solution is to define a SM4 encryption algorithm to encrypt and decrypt the data dynamically when reading and writing through UDF, other details are not detailed here, this article mainly describes how to develop a simple UDF with Go and use it.
Go Error Handling: A Guide to Using Error Chains
0. A brief review of Go error handling Go is a programming language with a strong emphasis on error handling. In Go, errors are represented as values of types that implement the error interface. The error interface has only one method:
1 2 3 type error interface { Error() string } The introduction of this interface allows Go programs to handle errors in a consistent and idiomatic manner.
One of the challenges of error handling in all programming languages is to be able to provide enough error context information to help the developer diagnose the problem while avoiding the developer being inundated with unnecessary details.
How to Configure JVM Memory in a Cloud Native Context
Background Some time ago, I received feedback that the application memory usage was very high, resulting in frequent restarts, so I was asked to investigate what was going on;
I didn’t pay much attention to this problem before, so I made a record of the process of checking and analyzing this time.
First, I checked the Pod monitoring in the monitoring panel.
It is found that it is indeed almost full, but at this point, when you go to check the JVM occupancy of the application, it is only about 30%; it means that it is not that the application memory is full that causes the OOM of the JVM, but that the Pod’s memory is full, causing the Pod’s memory to overflow and thus be killed by k8s.
Several ways to send IP packets with Go
We can easily send UDP and TCP packets using the net package in the Go standard library, and develop application layer programs based on them, such as HTTP, RPC and other frameworks and programs, and we can even use the official extension package golang.org/x/net/icmp , which is dedicated to sending and receiving icmp packets. However, sometimes we want to do lower level network communication, this time we need to use
Writing WebAssembly programs with Go
1. Introduction to WebAssembly Cross-platform, runs on any platform that supports WebAssembly, including web browsers, servers, mobile devices, etc. High performance, using a compact binary format that can be loaded and parsed quickly in a browser to improve application performance Security, using a sandbox model that isolates the code running in it to protect the system from malicious code attacks Portability, WebAssembly code can be generated from different programming languages through a compiler, so existing code can be easily converted to WebAssembly format Extensibility, WebAssembly supports backward-compatible version control and allows new instructions and features to be added in the future, making it more extensible 2.
Using mkcert to generate self-signed certificates
This blog uses Let's Encrypt + acme.sh to deploy HTTPS certificates, which has been running stably for a long time. Recently, when doing research related to browser fingerprinting, we found that the local HTTP environment cannot directly call some APIs, which is the Secure Context restriction mentioned in the previous article, this article will introduce a convenient solution to deploy self-signed HTTPS certificates in the local Web environment.
mkcert is a simple tool for making local self-signed HTTPS certificates that is very easy to use and does not even require additional configuration information.
Secure Context Restrictions for Browsers
Mainstream browsers have Secure Context enabled, which prohibits browsers from accessing content that is considered insecure, preventing information leakage and security issues caused by illegitimate access.
Secure Context Mainstream browsers have Secure Context, the secure contextual environment, enabled. The Secure Context specification defines minimum standards for authentication and confidentiality, and some APIs are restricted to run only in Secure Context. For example, accessing cameras, getting user password credentials, Service Worker, etc.
WebAssembly Serverless will become a common architecture in the future
1. Container-based Serverless cannot support the shape of next-generation applications As shown above, we are experiencing a change in runtime state.
From bare metal machines to virtual machines, applications are no longer limited by the number of local servers, server room stability, with better resiliency and availability.
From virtual machines to containers, applications are no longer limited by the operating system, configuration drift, with better portability and scalability.
What’s the next runtime?
Ping and Record Route
In the previous article, we implemented the basic functionality of the ping tool ourselves, and we learned about the principles of the underlying ping implementation. A reader asked a question: Is it possible to implement a one-way traceroute feature, similar to ping -R, that tracert B from A and show the path from B to A at the same time?
Record Route Background Knowledge I haven’t used the -R parameter of ping before, and it’s interesting to me that the reader asked this.
Static WEB container image minimization practice
In modern B/S architecture applications, we do front-end and back-end separation, and some front-end web services put the compiled static files to a web server for deployment. For example, my blog is also based on static files compiled by Hugo for deployment.
Then in containerized deployment mode, we need to build static files into a container image of a site or web service for deployment based on a base container (image) of the web service.
Memory card grade classification
The SD Card Association has defined four speed fraction levels Normal speed grade: C2, C4, C6, C10 UHS speed grade: U1, U3 Video speed grade: V6, V10, V30, V90 Application performance level: A1, A2 1. Class grade standard (normal speed grade) C0-C10 markings on memory cards
Early memory card speed grade using Class grade to identify. There are mainly C10, C6, C4 and C2 markings. C1 represents CLASS1, C10 represents CLASS10, and the number corresponding to C is equivalent to the corresponding number of CLASS.