Go/Quick Start: Difference between revisions
< Go
Jump to navigation
Jump to search
| (16 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"> | <syntaxhighlight lang="bash"> | ||
| Line 6: | Line 8: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Add source code. | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
| Line 16: | Line 19: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Run. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 21: | Line 26: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = Work with 2 modules = | ||
<syntaxhighlight lang="bash"> | |||
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> | |||
= naming convention table = | |||
{| class="wikitable" | |||
! Element | |||
! Visibility | |||
! Casing Rule | |||
|- | |||
| File | |||
| | |||
| snake_case | |||
|- | |||
| Package | |||
| | |||
| Lowercase | |||
|- | |||
| Module | |||
| | |||
| kebab-case | |||
|- | |||
| 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 | |||
|} | |||
Latest revision as of 09:40, 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
| Element | Visibility | Casing Rule |
|---|---|---|
| File | snake_case | |
| Package | Lowercase | |
| Module | kebab-case | |
| 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 |