Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 PROGRAMMING


  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{}...

6,381 2       EMPTY STRUCT GOLANG


  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...

787 0       GOLANG PPROF GOROUTINE LEAK DEBUG GUIDE SSH TIMEOUT


  Understanding Slice Behavior in Go

In Go, understanding how slices behave when passed to functions is crucial for writing efficient and bug-free code. This behavior is often a source of confusion for many developers, especially those new to the language. In this article, we'll explore the difference between passing slices by value and by reference, and how it impacts the modification of slices within functions.IntroductionIn Go, slices are a fundamental data structure used to work with sequences of elements. They are essentially a view into an underlying array, providing a flexible and powerful way to manipulate collections of ...

366 0       ARRAY SLICE PASS BY REFERENCE PASS BY VALUE


  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...

439 0       WINDOWS TUTORIAL GOLANG SQLITE3


  Break down defer statements in GoLang

Basic ConceptsWhat are the characteristics of the deferred statement defer in Go language? When is it usually used?The deferred statement(defer statement) in Go language has the following characteristics:Deferred Execution: Deferred statements are executed before the function containing them exits, regardless of whether the function returns normally or encounters an exception.Last In, First Out (LIFO): If there are multiple deferred statements, they are executed in the order of last in, first out (LIFO). In other words, the last deferred statement is executed first, and the first deferred stat...

678 0       DEFER GOLANG


  Rust vs Go: how to choose the best programming language for your project?

Rust and Go, these two modern programming languages, with their unique advantages, are becoming hot topics in the developer community. Their competition in performance, security, simplicity, feature set, and concurrency not only influences developers' choices but also foretells future trends in software development.Battle of Performance: Rust's Precision vs. Go's EfficiencyRust, developed by Mozilla Research, has become the preferred choice for performance-sensitive applications due to its zero-cost abstractions and memory safety features. It ensures memory safety through concepts like ownersh...

1,432 0       RUST GO GOLANG COMPARISON


  What is goroutine?

Casual TalkGolang is quite enjoyable to write, aside from the tedious if err != nil checks. One of the fundamental reasons for the joy is goroutine, a core feature of Golang. Understanding goroutines in detail is worthwhile, as they contribute significantly to the pleasure of working with Golang. So, let's talk about goroutines, hoping to provide some insights to the readers.TL;DR: We'll start by talking about assembling a computer, then delve into some concepts of the operating system, such as processes and threads, before exploring goroutines themselves.Assembling a ComputerLet's focus on de...

836 0       EXPLANATION GOLANG GOROUTINE


  Deep Dive into Spin Locks in Golang

In concurrent programming, a Mutex is a commonly used synchronization mechanism to protect critical resources and prevent data races. However, in certain specific scenarios, especially when the lock-holding time is short and the number of threads is limited, a more lightweight lock known as a Spin Lock can provide higher performance.What is a Spin LockA Spin Lock is a form of busy-wait lock. When a thread attempts to acquire a lock held by another thread, it continuously checks the lock's status (i.e., "spins") until the lock is released, at which point it takes ownership. This waiting method ...

789 0       SPINLOCK MUTEX GOLANG