From c508daeea56c16a46b0973b591e3df33ff8781b6 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Wed, 27 Mar 2024 11:08:52 +0800 Subject: [PATCH] Include pre-release identifiers in version handling --- .github/workflows/pull-request.yml | 4 +- docs/contrib/go-code.md | 71 +++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index ef712a27c..f7c5900ce 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -69,9 +69,9 @@ jobs: echo "Generate all necessary files successfully" continue-on-error: true - - name: Generate Vertions + - name: Generate Versions Including Pre-release Identifiers run: | - latest_tag=$(git describe --tags `git rev-list --tags --max-count=1` | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') + latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`) echo $latest_tag > pkg/common/config/version continue-on-error: true diff --git a/docs/contrib/go-code.md b/docs/contrib/go-code.md index 5c0212725..22f5b56e2 100644 --- a/docs/contrib/go-code.md +++ b/docs/contrib/go-code.md @@ -115,22 +115,83 @@ var s = F() func F() string { return "A" } ``` -- Use `_` as a prefix for unexported top-level constants and variables. +- This example emphasizes using PascalCase for exported constants and camelCase for unexported ones, avoiding all caps and underscores. ```go // bad const ( - defaultHost = "127.0.0.1" - defaultPort = 8080 + MAX_COUNT = 100 + timeout = 30 ) // good const ( - _defaultHost = "127.0.0.1" - _defaultPort = 8080 + MaxCount = 100 // Exported constants should use PascalCase. + defaultTimeout = 30 // Unexported constants should use camelCase. ) ``` +- Grouping related constants enhances organization and readability, especially when there are multiple constants related to a particular feature or configuration. + +```go +// bad +const apiVersion = "v1" +const retryInterval = 5 + +// good +const ( + ApiVersion = "v1" // Group related constants together for better organization. + RetryInterval = 5 +) +``` + +- The "good" practice utilizes iota for a clear, concise, and auto-incrementing way to define enumerations, reducing the potential for errors and improving maintainability. + +```go +// bad +const ( + StatusActive = 0 + StatusInactive = 1 + StatusUnknown = 2 +) + +// good +const ( + StatusActive = iota // Use iota for simple and efficient constant enumerations. + StatusInactive + StatusUnknown +) +``` + +- Specifying types explicitly improves clarity, especially when the purpose or type of a constant might not be immediately obvious. Additionally, adding comments to exported constants or those whose purpose isn't clear from the name alone can greatly aid in understanding the code. + +```go +// bad +const serverAddress = "localhost:8080" +const debugMode = 1 // Is this supposed to be a boolean or an int? + +// good +const ServerAddress string = "localhost:8080" // Specify type for clarity. +// DebugMode indicates if the application should run in debug mode (true for debug mode). +const DebugMode bool = true +``` + +- By defining a contextKey type and making userIDKey of this type, you avoid potential collisions with other context keys. This approach leverages Go's type system to provide compile-time checks against misuse. + +```go +// bad +const userIDKey = "userID" + +// In this example, userIDKey is a string type, which can lead to conflicts or accidental misuse because string keys are prone to typos and collisions in a global namespace. + + +// good +type contextKey string + +const userIDKey contextKey = "userID" +``` + + - Embedded types (such as mutexes) should be at the top of the field list within the struct, and there must be a blank line separating embedded fields from regular fields. ```go