Understanding Golang's Time Structure

When you create and print a time object in Golang, you’ll see an output like this: 2018-10-26 14:15:50.306558969 +0800 CST m=+0.000401093 The initial part is easy to understand, representing the year, month, day, time, and timezone. But what does the last part m=+xxxx signify? Monotonic Clocks and Wall Clocks According to the documentation of Golang’s time package, the time structure in Golang stores two types of clocks: Wall Clocks and Monotonic Clocks. ...

October 26, 2018 · 4 min · Zhiya

Stack Space Management in Go

Basic Logic of Stack Space Management The Go language provides support for concurrent programming through goroutines. Goroutines are a feature of the Go runtime library, not implemented as operating system threads, and can be understood as user-level threads. Since goroutines are managed by the Go runtime library, the library also needs to create and manage the corresponding stack space for each goroutine. The stack space allocated for each goroutine should not be too large, as it would waste a lot of space when many goroutines are created, nor too small, as it would lead to stack overflow. Go chooses a stack space management strategy where it starts with a relatively small space and automatically grows as needed. When a goroutine no longer needs such a large space, the stack space should also automatically shrink. ...

October 11, 2018 · 3 min · Zhiya

Pitfalls of 'defer' in Go

The defer statement is a very useful feature in Go, allowing a method to be delayed until the method enclosing it returns. In practical applications, defer can serve the role of try…catch… in other languages and can be used for handling cleanup operations such as closing file handles. Timing of defer Execution A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking. ...

September 14, 2018 · 4 min · Zhiya

Using gofmt to Format Code

For a programming language, code formatting is often a contentious issue. Different developers may have varying coding styles and habits. However, if all developers can use a unified format to write code, they can focus their valuable time on solving the problems the language is meant to address. Introduction to gofmt The Golang development team has established an official code style and introduced the gofmt tool (gofmt or go fmt) to help developers format their code to this unified style. gofmt is a CLI program that prioritizes reading from standard input. If a file path is provided, it will format that file. If a directory is provided, it will format all .go files in the directory. If no parameters are provided, it will format all .go files in the current directory. ...

July 17, 2018 · 4 min · Zhiya

Installing Golang Environment and Dependency Management

In 2015, Go 1.5 introduced an experimental vendor mechanism (which became enabled by default in Go 1.6 in 2016). The vendor mechanism involves adding a vendor folder to the project to store dependencies, allowing different projects to isolate their dependencies. Golang is a statically typed, compiled, concurrent programming language with garbage collection. Golang provides convenient installation packages, supporting Windows, Linux, and Mac systems. Download and Install Package The official Golang website is https://golang.org/. If the official site is inaccessible, you can visit https://golang.google.cn/. On the website, clicking on Download Go will take you to the download page, where installation packages for various systems are available, along with source code for compiling and installation. ...

July 10, 2018 · 3 min · Zhiya