From 8a315584e1f30867ff120398f0f73d227489368d Mon Sep 17 00:00:00 2001 From: medusa Date: Mon, 30 Jun 2025 08:32:11 +0000 Subject: [PATCH] Update tech_docs/golang_getting_started.md --- ...g_started.md => golang_getting_started.md} | 243 +++++++++++++++++- 1 file changed, 242 insertions(+), 1 deletion(-) rename tech_docs/{go_getting_started.md => golang_getting_started.md} (90%) diff --git a/tech_docs/go_getting_started.md b/tech_docs/golang_getting_started.md similarity index 90% rename from tech_docs/go_getting_started.md rename to tech_docs/golang_getting_started.md index fa82af9..10d93b2 100644 --- a/tech_docs/go_getting_started.md +++ b/tech_docs/golang_getting_started.md @@ -3833,4 +3833,245 @@ func main() { 5. **[Go net Package Documentation](https://pkg.go.dev/net)** 6. **[Go unix Package Documentation](https://pkg.go.dev/golang.org/x/sys/unix)** -These guides should help you get started with each package and provide a solid foundation for building more complex Go applications that interact with the operating system and network. \ No newline at end of file +These guides should help you get started with each package and provide a solid foundation for building more complex Go applications that interact with the operating system and network. + +--- + +You got it\! Here's a comprehensive Golang coding standards document, adhering to the established structure and principles, with a strong emphasis on Go's idiomatic practices and the tools that enforce them. + +----- + +# Golang Coding Standards Documentation + +## Introduction + +This document outlines our conventions and best practices for writing **Golang (Go) code**. By adhering to these standards, we ensure our projects are **consistent, readable, maintainable, and performant**. This approach fosters **seamless team collaboration**, **reduces debugging time**, and **improves overall code reliability and scalability**. + +Our guidelines are heavily influenced by the official Go documentation, including ["Effective Go"](https://go.dev/doc/effective_go) and ["Go Code Review Comments"](https://go.dev/wiki/CodeReviewComments), and are designed to be enforced by standard Go tools. + +----- + +## Code Formatting + +Go has strong opinions on formatting, largely enforced by its tooling. + +### 1\. `gofmt` and `goimports` + + * **Description**: **Always run `gofmt`** on your code. It automatically formats Go source code according to the official style. **Always run `goimports`** (which includes `gofmt`) to also manage import paths (adding missing ones, removing unused ones, and organizing them). + * **Benefit**: Eliminates debates over formatting, ensures consistency across the entire codebase, and streamlines dependencies. + * **Usage**: Configure your IDE/editor to run `goimports` on save. From the command line: + ```bash + goimports -w . + ``` + +### 2\. Line Length + + * **No Hard Limit**: Unlike some languages, Go doesn't have a strict line length limit (like 80 characters). However, strive for **reasonable line lengths** that are easy to read without horizontal scrolling. If a line becomes too long, consider breaking it into multiple lines using natural Go syntax (e.g., after commas, operators, opening parentheses). + * **Benefit**: Promotes readability and avoids awkward line breaks that `gofmt` might otherwise introduce. + +### 3\. Blank Lines + + * **Logical Grouping**: Use blank lines to separate logical sections of code within functions or between related function definitions. + * **Imports**: `goimports` will handle blank lines between standard library, third-party, and internal imports automatically. + * **Usage**: + ```go + package main + + import ( + "fmt" // Standard library + "net/http" + "time" + + "github.com/gin-gonic/gin" // Third-party + ) + + // Two blank lines before top-level declarations + type User struct { + ID int + Name string + } + + func main() { + // One blank line for logical separation + router := gin.Default() + router.GET("/users/:id", getUserHandler) + router.POST("/users", createUserHandler) + + // Another logical separation + server := &http.Server{ + Addr: ":8080", + Handler: router, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + } + + fmt.Println("Server starting on :8080") + server.ListenAndServe() + } + ``` + +----- + +## Naming Conventions + +Go's naming conventions are crucial, especially for controlling visibility (exporting). + +### 1\. Visibility (Exporting) + + * **`PascalCase` (first letter capitalized)**: + * **Description**: For **exported** (publicly visible) names: packages, types, functions, methods, and variables that should be accessible from outside their defining package. + * **Usage**: `MyStruct`, `GetUserByID`, `ErrNotFound` + * **`camelCase` (first letter lowercase)**: + * **Description**: For **unexported** (private to the package) names: types, functions, methods, and variables. + * **Usage**: `myHelperFunction`, `userCache`, `errorHandler` + +### 2\. General Naming Guidelines + + * **Descriptive and Concise**: Names should be clear but also succinct. Avoid unnecessary verbosity. + * **Bad**: `get_user_by_unique_identifier_from_database` + * **Good**: `GetUserByID`, `fetchUser` + * **Acronyms**: Keep acronyms (e.g., `ID`, `HTTP`, `URL`, `API`) consistently cased. + * **Exported**: `userID`, `httpClient` (NOT `userId`, `httpClient`) + * **Unexported**: `apiConfig` (NOT `APIConfig` unless it's a type) + * **Receiver Names (Methods)**: + * **Description**: In method declarations, the receiver variable name should be short, usually one or two letters, and consistent across methods of the same type. + * **Usage**: `(u *User)`, `(s *Server)` + * **Interface Names**: + * **Description**: If an interface has a single method, its name is typically the method name plus `er` (e.g., `Reader`, `Writer`, `Closer`). + * **Usage**: `io.Reader`, `fmt.Stringer` + * **Package Names**: + * **Description**: Package names should be short, all lowercase, and reflect the package's purpose (e.g., `http`, `fmt`, `json`, `user`, `auth`). Avoid `_` or `.` in package names. + * **Usage**: `package main`, `package models`, `package utils` + +----- + +## Comments and Docstrings + +Good documentation in Go means clear explanations for exported entities. + +### 1\. Comments (`//`) + + * **Descriptive**: Use comments to explain **why** a piece of code exists, or to clarify complex logic. Avoid commenting on obvious code. + * **Concise**: Keep comments brief and to the point. + * **Up-to-Date**: Ensure comments are updated if the code changes. + * **Usage**: + ```go + // Loop through connections, attempting to reconnect with exponential backoff. + for i := 0; i < maxRetries; i++ { + if err := connectToDatabase(); err == nil { + break // Successfully connected + } + time.Sleep(time.Duration(2<