The Correct Way to Use `go build`

When working with Go, it’s important to know the proper way to compile your programs to avoid common errors. Here are some tips on using the go build command effectively. Recommended Usage Compile all Go files in the current directory: go build Compile all Go files explicitly: go build *.go Common Pitfalls Compiling a Single File Running: go build main.go will only build main.go. This can lead to errors if main.go depends on other Go files in the same package, as those files won’t be included in the build process. ...

November 28, 2024 · 1 min · 163 words · Jack Yu

Using delve to Debug Golang Programs

Background When I first started writing Golang, I was always looking for a convenient debugging tool. Back then, I came across documentation about using gdb to debug and also tried delve, but neither felt easy to use. Later, on someone’s advice, I went back to the good old print statements… Over the past couple of days, I was debugging go test and found that tests would always hang when run per package. I couldn’t think of a suitable method at first, so I thought of delve again. After giving it a try, I found it has become much more mature than before. ...

September 16, 2019 · 3 min · 613 words · Jack Yu

How Immediate is Golang's Panic

Let’s first look at the following code snippet: package main import ( "fmt" "os" "runtime" "time" ) func main() { runtime.GOMAXPROCS(2) a := make(map[int]int) go func() { i := 0 for { a[1] = i i++ time.Sleep(1000) } }() for { if a[1] > 1000000 { fmt.Println(a[1]) os.Exit(1) } } } After compiling and running it (assuming your machine has 2 or more cores), you will get the following error: fatal error: concurrent map read and map write goroutine 1 [running]: runtime.throw(0x10c3e05, 0x21) /usr/local/Cellar/go/1.10.3/libexec/src/runtime/panic.go:616 +0x81 fp=0xc42004bf00 sp=0xc42004bee0 pc=0x10263f1 runtime.mapaccess1_fast64(0x10a5b60, 0xc42007e180, 0x1, 0xc42008e048) /usr/local/Cellar/go/1.10.3/libexec/src/runtime/hashmap_fast.go:101 +0x197 fp=0xc42004bf28 sp=0xc42004bf00 pc=0x1008d27 main.main() /Users/yusp/test/panic3/main.go:22 +0x7c fp=0xc42004bf88 sp=0xc42004bf28 pc=0x108e28c runtime.main() /usr/local/Cellar/go/1.10.3/libexec/src/runtime/proc.go:198 +0x212 fp=0xc42004bfe0 sp=0xc42004bf88 pc=0x1027c62 runtime.goexit() /usr/local/Cellar/go/1.10.3/libexec/src/runtime/asm_amd64.s:2361 +0x1 fp=0xc42004bfe8 sp=0xc42004bfe0 pc=0x104e501 goroutine 5 [runnable]: time.Sleep(0x3e8) /usr/local/Cellar/go/1.10.3/libexec/src/runtime/time.go:102 +0x166 main.main.func1(0xc42007e180) /Users/yusp/test/panic3/main.go:18 +0x61 created by main.main /Users/yusp/test/panic3/main.go:13 +0x59 It seems straightforward; Golang’s map is not thread-safe, and concurrent read and write cause a panic. However, look at the error information on line /Users/yusp/test/panic3/main.go:18 +0x61, which points to line 18 of main.go where Sleep is called, not the actual point of concurrency issue. In a vast stack trace, it becomes even harder to locate the problem. ...

July 26, 2018 · 2 min · 238 words · Jack Yu