1func interceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { 2 start := time.Now() 3 err := invoker(ctx, method, req, reply, cc, opts...) 4 logger.Infof("[gRPC] method=%s req=%v rep=%+v duration=%s error=%v", method, req, reply, time.Since(start), err) 5 if err != nil { 6 return err 7 } 8 9 return nil 10} In a gRPC interceptor, I wanted to add a check to determine if the server’s response is nil to avoid subsequent panic when accessing fields (I didn’t want to check every time).
...
1. Scenario When using YAML as a configuration file, it is often necessary to parse the configuration when starting a service. If the data structure of a field is modified, the service might panic immediately. Sometimes, we do not want the business to stop at this point and hope to replace it with a default configuration file.
1# cfg.yaml 2language: Golang 3animal: Dog 1type Cfg struct { 2 Language string `json:"language,omitempty"` 3 Animal string `json:"animal,omitempty"` 4} Under normal circumstances, the YAML file will be parsed successfully as follows:
...
1. Variable Declaration Variable declaration refers to informing the compiler about the type and name of a variable without allocating memory or initializing it. In Go, variable declaration is typically done using the var keyword.
1var x int In this example, x is declared as a variable of type int, but at this point, x is not assigned any initial value. Go will allocate memory for x and initialize it to the zero value of the int type (i.
...
TCP Protocol You need to establish two session windows - one as TCP Server and another as TCP Client for communication between them. Note that sometimes you may encounter bind() failure errors, which usually occurs when the specified port is already in use. You can use the previously mentioned cmd commands to check and resolve this issue.
UDP Protocol UDP doesn’t require handshaking connection. After clicking connect, simply specify the remote host and port to send and receive data.
...
kill 1$ kill pid # kill -15 pid default kill The system sends a SIGTERM signal to the corresponding program. When the program receives this signal:
The program stops immediately. The program stops after releasing its resources. The program may continue running. Most programs will release their resources before stopping after receiving the SIGTERM signal. However, some programs can perform other configurable actions upon receiving the signal. If the program is waiting for I/O, it may not respond immediately.
...
In the Go language, Map is implemented based on a Hash Table. The core idea of a hash table is to map a key to a storage location (bucket) using a hash function, enabling fast data lookup, insertion, and deletion. Basic Concepts Map Also known as a dictionary, hash table, or associative array, a map is a widely used data structure in computer science for storing key-value pairs, where each
...
Golang 中 Slice 实现及源码分析 数组 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列,这种类型可以是任意的原始类型例如整形、字符串或者自定义类型
...
值类型和引用类型 在 Go 语言中,数据类型可以分为 值类型 和 引用类型。这两种类型的主要区别在于它们在赋值、传递和存储时的行为方式。 值类型 (Value Types) 值类型的
...