A different Go project version number management solution
#So, how to add a version number to the project? Many people should have used hard coding, that is, writing the version number directly into the source code or configuration file, and modifying the version number every time the function is upgraded. This method is obviously feasible, but it is also error-prone. First, it is easy to forget to update the version number when releasing a version. Second, when multiple branch codes are merged, there may be confusion.
The following will bring you a different management plan.
ldflags -X variable transfer
go linker Linker is a tool for assembling binary files. When we execute the go build command, we can pass <span style="font-size: 15px;">--ldflags</span>
Flags set linker parameters. Use the following statement to view the linker optional parameters.
go build --ldflags="--help"
There are many parameters, but what we are interested in is<span style="font-size: 15px;">-X</span>
$ go build --ldflags="--help" usage: link [options] main.o ... -X definition add string value definition of the form importpath.name=value ...
<span style="font-size: 15px;">-X</span>
parameter, specify importpath.name=value, used to modify the variable value. Among them, importpath represents the package import path, name is the variable name in the program, and value represents the variable value we want to set.
下面,我们通过示例项目来具体感受一下。
$ mkdir versionDemo $ cd versionDemo/ $ go mod init versiondemo go: creating new go.mod: module versiondemo $ touch main.go
在 main 函数中,我们打印 version 值。
package main import ( "fmt" ) var ( version = "0.0.1" ) func main() { fmt.Println("version: ", version) }
如果正常编译执行程序,将得到以下结果
$ go build -o main && ./main version: 0.0.1
此时,我们指定 --ldflags 的 -X 参数重新编译执行
$ go build -o main --ldflags="-X 'main.version=client-0.0.2'" && ./main version: client-0.0.2
可以看到 version 参数值已经被改变。
添加 git 信息
开发中需要使用 git 工具,本文讨论的版本管理,也经常与 git tag 挂钩。那其实有更酷的操作:我们可以在构建期间,通过 git commit 信息自动填充版本号。
我们基于上文项目目录,添加 git commit 信息。
$ git init $ git add . $ git commit -m "initial commit"
通过以下命令,可拿到 git commit 的 hash 值
$ git rev-parse HEAD 46dab0ddb6ba20445c2c1f047575e25d3aad1a27
该值较长,我们可以添加 --short 选项获取短 hash 值。
$ git rev-parse --short HEAD 46dab0d
此时,通过指定 --ldflags 的 -X 参数,将 version 值替换成 git commit 的哈希值。这样,我们成功地将项目版本与 git 信息绑定在了一起。
$ go build -o main --ldflags="-X 'main.version=$(git rev-parse --short HEAD)'" && ./main version: 46dab0d
总结
本文介绍了一种如何通过 ldflags -X 变量传递的方式。使用这种方式我们可以在构建时轻松设定一些元信息,例如本文示例的程序版本信息。而这种构建的动作不应该手动去执行,而是放入到 CI/CD 流程中,让整个过程变得更加丝滑。
The above is the detailed content of A different Go project version number management solution. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).

In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

View Go function documentation using the IDE: Hover the cursor over the function name. Press the hotkey (GoLand: Ctrl+Q; VSCode: After installing GoExtensionPack, F1 and select "Go:ShowDocumentation").

In Golang, error wrappers allow you to create new errors by appending contextual information to the original error. This can be used to unify the types of errors thrown by different libraries or components, simplifying debugging and error handling. The steps are as follows: Use the errors.Wrap function to wrap the original errors into new errors. The new error contains contextual information from the original error. Use fmt.Printf to output wrapped errors, providing more context and actionability. When handling different types of errors, use the errors.Wrap function to unify the error types.

Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.
