ALL
Go 1.22 for-range Fix and go.mod Dependency
With the release of Go 1.22, the Go team addressed a long-standing issue with the for-range loop variable scoping, which had been a common pitfall for developers writing concurrent code. Specifically, the problem was that the loop variable in a for-range loop was reused across iterations, leading to unexpected behavior when closures were involved.The Issue Before Go 1.22Consider the following example:package forangeimport "fmt"func Do() { done := make(chan bool) values := []string{"a", "b", "c"} for _, v := range values { go func() { fmt.Println(v) // Prints unexpect...
Why is Golang's Compilation Speed So Fast?
OverviewWhen I started learning the Go language, I already had experience with three statically typed languages—C/C++ and Java—and two dynamically typed languages—PHP and JavaScript. Because of this background, when I compiled a demo file of several hundred lines for the first time in Go, I was genuinely impressed by its compilation speed. At that moment, I thought to myself, "Go claims to have the execution speed of statically typed languages and the compilation speed of dynamically typed languages—it indeed lives up to its reputation." Indeed, one of the primary motiv...
2,922 0 GO COMPILATION GOLANG
Introduction to the Application of eBPF in Golang
Most of the time, when we develop software or even use software, we play within the safe boundaries of the operating system. We might not know how the network interface welcomes that IP packet, nor how the filesystem handles the inodes when we save a file.This boundary is called user space, which is where we write applications, libraries, and tools. But there's another world, kernel space, where the operating system's kernel resides and is responsible for managing system resources such as memory, CPU, and I/O devices.We usually don’t need to go below the socket or file descriptor level, ...
1,549 0 EBPF GOLANG GUIDE APPLICATION
GoLang Interview Questions
Below lists some frequently asked GoLang interview questions and their corresponding answers based on the author's experience. The list is updated frequently with new understandings. Stay tuned.What is the GMP model of GoLang?GoLang uses goroutine to achieve concurrency and it is famous for high concurrency support as the language defines its own goroutine dispatching and processing system which is well known as GMP model.How it works is that M is normally defined as the OS thread being spawned( then each M thread should be associated with a Process queue and each Process queue can have multip...
Why init() is not recommended in Go
golangci lintCurrently, the unified CI's .golangci.yml includes the gochecknoinits checker:# golangci-lint v1.46.2# https://golangci-lint.run/usage/linterslinters: disable-all: true enable: ... - gochecknoinits # Checks that no init functions are present in Go code. ref: https://github.com/leighmcculloch/gochecknoinits ...If Go code uses the init() function, the following error will occur:# golangci-lint runfoo/foo.go:3:1: don't use `init` function (gochecknoinits)func init() {^That is, it's not recommended to use init().Why not to use init()Searching "golang why not use init" on G...
What is the use of empty struct in GoLang
In Go, an empty struct struct{} is a struct with no fields that may appear to be of little use, but in reality, it can be useful in certain situations and become a simple and efficient solution in code.As a semaphore or lockBecause the empty struct has no fields, it can be conveniently used to implement some concurrency control functions, such as mutex locks, read-write locks. We can use chan struct{} to implement an unbuffered channel for controlling concurrent access.package mainimport ( "fmt" "sync")func main() { var wg sync.WaitGroup var mu sync.Mutex c := make(chan struct{}...
9,484 2 GOLANG EMPTY STRUCT
A journey to investigate a goroutine leakage case
In Go, creating goroutines is straightforward, but improper usage may result in a large number of goroutines unable to terminate, leading to resource leakage and memory leaks over time. The key to avoiding goroutine leaks is to manage the lifecycle of goroutines properly. By exporting runtime metrics and utilizing pprof, one can detect and resolve goroutine leakage issues.This post will go through one real case encountered by the author. The author maintains a service that connects to a target machine via SSH and executes commands. This is an internal service that is usually not closely monito...
Connect to SQLite using Go on Windows
In software development, it's often necessary to test and validate logic using a lightweight and easily manageable database system. SQLite, with its minimal setup and zero configuration requirements, is an excellent choice for such scenarios. Leveraging the simplicity and efficiency of SQLite for testing purposes can significantly streamline the development process. In this guide, we'll explore how to seamlessly connect to SQLite using Go on Windows, empowering developers to efficiently test their code with a reliable and straightforward database solution.Install SQLIte3 PackagesGo has built-i...