Go source code files
While there isn't a filename convention for Go source code files, their filenames are typically one-word, all lowercase, and include an underscore if it has more than one word. It ends with the .go
suffix.
Each file has three parts:
- Package clause: This defines the name of the package a file belongs to.
- Import declaration: This is a list of packages that you need to import.
- Top-level declaration: This is constant, variable, type, function, and method declarations with a package scope. Every declaration here starts with a keyword (
const
,var
,type
, orfunc
):
// package clause
package main
// import declaration
import "fmt"
// top level declaration
const s = "Hello, 世界"
func main() {
fmt.Println(s)
}
The code example shows the package declaration for the main
package at the top. It follows the import declaration, where we specify that we use the fmt
package in this file. Then, we include all declarations in the code –...