Go/Quick Start: Difference between revisions

From Fundamental Ramen
< Go
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Create a module =
= Create a module =
Create module and source file.
<syntaxhighlight lang="bash">
go mod init sandbox/hello
touch hello.go
</syntaxhighlight>
Add source code.
<syntaxhighlight lang="go">
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
</syntaxhighlight>
Run.
<syntaxhighlight lang="bash">
go run .
</syntaxhighlight>
= Work with 2 modules =


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
go mod init example.com/m
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
</syntaxhighlight>
</syntaxhighlight>


= Run =
= naming convention table =
 
TODO: Use TreeView to fix
 
{| class="wikitable"
! Element
! Visibility
! Casing Rule
! Example
! Notes
|-
| Package
| N/A
| Lowercase
| package utils
| Packages are namespaces. They must be concise and descriptive.
|-
| Module
| N/A
| 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
| Exported (Public)
| PascalCase
| type User struct
| Used to define public data structures.
|-
| Struct Field
| Exported (Public)
| PascalCase
| Name string
| Fields starting with a capital letter are visible outside the package.
|-
| Struct Field
| Unexported (Private)
| camelCase
| age int
| Fields starting with a lowercase letter are only visible within the package.
|-
| Function
| Exported (Public)
| PascalCase
| func GetUser() User
| Functions starting with a capital letter are public.
|-
| Function
| Unexported (Private)
| camelCase
| func internalHelper()
| Functions starting with a lowercase letter are private to the package.
|-
| Variable
| Exported (Public)
| PascalCase
| var MaxUsers int
| Variables starting with a capital letter are public.
|-
| Variable
| Unexported (Private)
| camelCase
| var count int
| Variables starting with a lowercase letter are private to the package.
|-
| Constant
| Exported (Public)
| ALL_CAPS
| const MaxUsers = 100
| Used for fixed, immutable values.
|-
| Interface
| Exported (Public)
| PascalCase
| type Reader interface
| Interfaces are typically named to describe the action they perform (e.g., Reader, Writer).
|-
| Method
| Exported/Unexported
| Follow Function Rules
| func (u *User) GetName() string
| Methods follow the same visibility rules as functions.
|}

Latest revision as of 03:05, 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 Example Notes
Package N/A Lowercase package utils Packages are namespaces. They must be concise and descriptive.
Module N/A 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 Exported (Public) PascalCase type User struct Used to define public data structures.
Struct Field Exported (Public) PascalCase Name string Fields starting with a capital letter are visible outside the package.
Struct Field Unexported (Private) camelCase age int Fields starting with a lowercase letter are only visible within the package.
Function Exported (Public) PascalCase func GetUser() User Functions starting with a capital letter are public.
Function Unexported (Private) camelCase func internalHelper() Functions starting with a lowercase letter are private to the package.
Variable Exported (Public) PascalCase var MaxUsers int Variables starting with a capital letter are public.
Variable Unexported (Private) camelCase var count int Variables starting with a lowercase letter are private to the package.
Constant Exported (Public) ALL_CAPS const MaxUsers = 100 Used for fixed, immutable values.
Interface Exported (Public) PascalCase type Reader interface Interfaces are typically named to describe the action they perform (e.g., Reader, Writer).
Method Exported/Unexported Follow Function Rules func (u *User) GetName() string Methods follow the same visibility rules as functions.