Go/Quick Start: Difference between revisions

From Fundamental Ramen
< Go
Jump to navigation Jump to search
Line 49: Line 49:
! Visibility
! Visibility
! Casing Rule
! Casing Rule
! Example
! Notes
|-
|-
| File
| File
|
|
| snake_case
| snake_case
|
|
|-
|-
| Package
| Package
| N/A
| N/A
| Lowercase
| Lowercase
| package utils
| Packages are namespaces. They must be concise and descriptive.
|-
|-
| Module
| Module
| N/A
| N/A
| kebab-case (Lowercase with hyphens)
| kebab-case (Lowercase with hyphens)
| github.com/user/my-project
| Used for import paths and module names. Kebab-case is standard for URLs.
|-
|-
| Struct / Type
| Struct / Type
| Exported (Public)
| Exported (Public)
| PascalCase
| PascalCase
| type User struct
| Used to define public data structures.
|-
|-
| Struct Field
| Struct Field
| Exported (Public)
| Exported (Public)
| PascalCase
| PascalCase
| Name string
| Fields starting with a capital letter are visible outside the package.
|-
|-
| Struct Field
| Struct Field
| Unexported (Private)
| Unexported (Private)
| camelCase
| camelCase
| age int
| Fields starting with a lowercase letter are only visible within the package.
|-
|-
| Function
| Function
| Exported (Public)
| Exported (Public)
| PascalCase
| PascalCase
| func GetUser() User
| Functions starting with a capital letter are public.
|-
|-
| Function
| Function
| Unexported (Private)
| Unexported (Private)
| camelCase
| camelCase
| func internalHelper()
| Functions starting with a lowercase letter are private to the package.
|-
|-
| Variable
| Variable
| Exported (Public)
| Exported (Public)
| PascalCase
| PascalCase
| var MaxUsers int
| Variables starting with a capital letter are public.
|-
|-
| Variable
| Variable
| Unexported (Private)
| Unexported (Private)
| camelCase
| camelCase
| var count int
| Variables starting with a lowercase letter are private to the package.
|-
|-
| Constant
| Constant
| Exported (Public)
| Exported (Public)
| ALL_CAPS
| ALL_CAPS
| const MaxUsers = 100
| Used for fixed, immutable values.
|-
|-
| Interface
| Interface
| Exported (Public)
| Exported (Public)
| PascalCase
| PascalCase
| type Reader interface
| Interfaces are typically named to describe the action they perform (e.g., Reader, Writer).
|-
|-
| Method
| Method
| Exported/Unexported
| Exported/Unexported
| Follow Function Rules
| Follow Function Rules
| func (u *User) GetName() string
| Methods follow the same visibility rules as functions.
|}
|}

Revision as of 09:39, 27 July 2026

Create a module

Create module and source file.

go mod init sandbox/hello
touch hello.go

Add source code.

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}

Run.

go run .

Work with 2 modules

mkdir main greetings sucks
cd greetings
go mod init sandbox/greetings
cd ../sucks
go mod init sandbox/sucks
cd ../main
go mod init
go mod edit -replace sandbox/greetings=../greetings
go mod edit -replace sandbox/sucks=../sucks
go mod tidy

naming convention table

TODO: Use TreeView to fix

Element Visibility Casing Rule
File snake_case
Package N/A Lowercase
Module N/A kebab-case (Lowercase with hyphens)
Struct / Type Exported (Public) PascalCase
Struct Field Exported (Public) PascalCase
Struct Field Unexported (Private) camelCase
Function Exported (Public) PascalCase
Function Unexported (Private) camelCase
Variable Exported (Public) PascalCase
Variable Unexported (Private) camelCase
Constant Exported (Public) ALL_CAPS
Interface Exported (Public) PascalCase
Method Exported/Unexported Follow Function Rules