mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-07 18:45:58 +08:00
refactor: 3.7.0 code conventions. (#2148)
* Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * feat: add code lint * feat: add code lint * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * Script Refactoring * feat: code format * Script Refactoring * Script Refactoring * Script Refactoring * Adjust MinIO configuration settings * Adjust configuration settings * Adjust configuration settings * refactor: config change. * refactor: webhooks update. * Adjust configuration settings * refactor: webhooks update. * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * feat: s3 api addr * refactor: webhooks update. * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * Adjust configuration settings * refactor: webhooks update. * refactor: kafka update. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * refactor: kafka update. * refactor: kafka update. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * Simplify the Docker Compose configuration, remove unnecessary environment variables, and eliminate the gateway service. * Windows can compile and run. * Windows can compile and run. * refactor: kafka update. * feat: msg cache split * refactor: webhooks update * refactor: webhooks update * refactor: friends update * refactor: group update * refactor: third update * refactor: api update * refactor: crontab update * refactor: msggateway update * mage * mage * refactor: all module update. * check * refactor: all module update. * load config * load config * load config * load config * refactor: all module update. * refactor: all module update. * refactor: all module update. * refactor: all module update. * refactor: all module update. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * refactor: all module update. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * update tools * update tools * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * update protocol * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: all module update. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * Optimize Docker configuration and script. * refactor: api remove token auth by redis directly. * Code Refactoring * refactor: websocket auth change to call rpc of auth. * refactor: kick online user and remove token change to call auth rpc. * refactor: kick online user and remove token change to call auth rpc. * refactor: remove msggateway redis. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor webhook * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor webhook * refactor: cmd update. * refactor: cmd update. * fix: runtime: goroutine stack exceeds * refactor: cmd update. * refactor notification * refactor notification * refactor * refactor: cmd update. * refactor: cmd update. * refactor * refactor * refactor * protojson * protojson * protojson * go mod * wrapperspb * refactor: cmd update. * refactor: cmd update. * refactor: cmd update. * refactor: context update. * refactor: websocket update info. * refactor: websocket update info. * refactor: websocket update info. * refactor: websocket update info. * refactor: api name change. * refactor: debug info. * refactor: debug info. * refactor: debug info. * fix: update file * refactor * refactor * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * refactor: debug info. * fix: callback update. * fix: callback update. * refactor * fix: update message. * fix: msg cache timeout. * refactor * refactor * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: push update. * fix: websocket handle error remove when upgrade error. --------- Co-authored-by: skiffer-git <44203734@qq.com> Co-authored-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com> Co-authored-by: withchao <993506633@qq.com>
This commit is contained in:
+89
-26
@@ -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
|
||||
@@ -274,8 +335,6 @@ The use of `panic` should be carefully controlled in Go applications to ensure p
|
||||
|
||||
- **Restricted Use in Main Package:** In the main package, the use of `panic` should be reserved for situations where the program is entirely inoperable, such as failure to open essential files, inability to connect to the database, or other critical startup issues. Even in these scenarios, prefer using structured error handling to terminate the program.
|
||||
|
||||
- **Use `log.Fatal` for Critical Errors:** Instead of panicking, use `log.Fatal` to log critical errors that prevent the program from operating normally. This approach allows the program to terminate while ensuring the error is properly logged for troubleshooting.
|
||||
|
||||
- **Prohibition on Exportable Interfaces:** Exportable interfaces must not invoke `panic`. They should handle errors gracefully and return errors as part of their contract.
|
||||
|
||||
- **Prefer Errors Over Panic:** It is recommended to use error returns instead of panic to convey errors within a package. This approach promotes error handling that integrates smoothly with Go's error handling idioms.
|
||||
@@ -303,7 +362,7 @@ func SIGTERMExit() {
|
||||
|
||||
```go
|
||||
import (
|
||||
_ "net/http/pprof"
|
||||
_ "net/webhook/pprof"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
|
||||
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"
|
||||
@@ -357,27 +416,31 @@ The naming convention is a very important part of the code specification. A unif
|
||||
- Don't use broad, meaningless package names like common, util, shared or lib.
|
||||
- The package name should be simple and clear, such as net, time, log.
|
||||
|
||||
### 2.2 Function Naming
|
||||
|
||||
- The function name is in camel case, and the first letter is uppercase or lowercase according to the access control decision,For example: `MixedCaps` or `mixedCaps`.
|
||||
- Code automatically generated by code generation tools (such as `xxxx.pb.go`) and underscores used to group related test cases (such as `TestMyFunction_WhatIsBeingTested`) exclude this rule.
|
||||
### 2.2 Function Naming Conventions
|
||||
|
||||
In accordance with the naming conventions adopted by OpenIM and drawing reference from the Google Naming Conventions as per the guidelines available at https://google.github.io/styleguide/go/, the following expectations for naming practices within the project are set forth:
|
||||
Function names should adhere to the following guidelines, inspired by OpenIM’s standards and Google’s Go Style Guide:
|
||||
|
||||
1. **File Names:**
|
||||
+ Both hyphens (`-`) and underscores (`_`) are permitted when naming files.
|
||||
+ A preference is established for the use of underscores (`_`), suggesting it as the best practice in general scenarios.
|
||||
2. **Script and Markdown Files:**
|
||||
+ For shell scripts (bash files) and Markdown (`.md`) documents, the use of hyphens (`-`) is recommended.
|
||||
+ This recommendation is based on the improved searchability and compatibility in web browsers when hyphens are used in names.
|
||||
3. **Directories:**
|
||||
+ A consistent approach is mandated for naming directories, exclusively using hyphens (`-`) to separate words within directory names.
|
||||
- Use camel case for function names. Start with an uppercase letter for public functions (`MixedCaps`) and a lowercase letter for private functions (`mixedCaps`).
|
||||
- Exceptions to this rule include code automatically generated by tools (e.g., `xxxx.pb.go`) and test functions that use underscores for clarity (e.g., `TestMyFunction_WhatIsBeingTested`).
|
||||
|
||||
### 2.3 File and Directory Naming Practices
|
||||
|
||||
To maintain consistency and readability across the OpenIM project, observe the following naming practices:
|
||||
|
||||
**File Names:**
|
||||
- Use underscores (`_`) as the default separator in filenames, keeping them short and descriptive.
|
||||
- Both hyphens (`-`) and underscores (`_`) are allowed, but underscores are preferred for general use.
|
||||
|
||||
**Script and Markdown Files:**
|
||||
- Prefer hyphens (`-`) for shell scripts and Markdown (`.md`) files to enhance searchability and web compatibility.
|
||||
|
||||
**Directories:**
|
||||
- Name directories with hyphens (`-`) exclusively to separate words, ensuring consistency and readability.
|
||||
|
||||
Remember to keep filenames lowercase and use meaningful, concise identifiers to facilitate better organization and navigation within the project.
|
||||
|
||||
|
||||
### 2.3 File Naming
|
||||
|
||||
- Keep the filename short and meaningful.
|
||||
- Filenames should be lowercase and use underscores to separate words.
|
||||
|
||||
### 2.4 Structure Naming
|
||||
|
||||
@@ -478,9 +541,9 @@ var LintGonicMapper = GonicMapper{
|
||||
- If the variable type is bool, the name should start with Has, Is, Can or Allow, for example:
|
||||
|
||||
```go
|
||||
var has Conflict bool
|
||||
var hasConflict bool
|
||||
var isExist bool
|
||||
var can Manage bool
|
||||
var canManage bool
|
||||
var allowGitHook bool
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user