Every Golang source file begins with a package declaration. A package is the basic unit of organization in Golang. Files in the same directory normally belong to the same package and can share unexported identifiers with each other.
Executable programs use package main. The runtime starts the program by calling a function named main inside that package. Library packages use names that describe what they provide, such as users, orders, auth, or storage.
package main
import "fmt"
func main() {
fmt.Println("Hello from Golang")
}
Golang does not use public, private, or protected keywords. Visibility is controlled by the first letter of an identifier. If a function, type, variable, constant, struct field, or method starts with a capital letter, it is exported and can be used from another package.
If the name starts with a lowercase letter, it is available only inside the same package. This rule is simple, but it is important because it shapes how Golang projects expose APIs.
| Name | Visible Outside Package? | Typical Use |
|---|---|---|
User | Yes | Public model type |
NewUser | Yes | Constructor-style function |
validateEmail | No | Internal helper |
db | No | Package-private variable |
Imports tell Golang which packages a file depends on. A file cannot import a package and then ignore it. Unused imports are compile errors, which keeps code clean and prevents hidden dependencies from building up.
Single imports can be written on one line. Multiple imports are usually grouped in parentheses. Standard library packages such as fmt, time, strings, net/http, and encoding/json appear often in real applications.
package main
import (
"fmt"
"strings"
"time"
)
func main() {
title := strings.ToUpper("golang syntax")
fmt.Println(title)
fmt.Println("Generated at:", time.Now())
}
Functions begin with the func keyword. Parameters are written as name followed by type, and the return type appears after the parameter list. The opening brace must stay on the same line as the function declaration.
Statements usually do not need semicolons. The compiler inserts semicolons automatically at line breaks in specific situations. Because of that rule, moving braces to a separate line can produce syntax errors.
package main
import "fmt"
func formatCourse(name string, lessons int) string {
return fmt.Sprintf("%s has %d lessons", name, lessons)
}
func main() {
message := formatCourse("Golang", 16)
fmt.Println(message)
}
Use // for single-line comments and /* */ for block comments. Comments should explain intent, constraints, or surprising behavior. Avoid repeating what the code already says.
Exported identifiers should have documentation comments that start with the identifier name. Tools such as go doc and editor hovers use these comments to show helpful documentation.
package course
// Course stores public information about a learning track.
type Course struct {
Title string
Lessons int
}
// IsLong reports whether the course has many lessons.
func (c Course) IsLong() bool {
return c.Lessons >= 20
}
gofmt is one of the most important Golang tools. It formats source files in one standard style, so teams do not waste time debating indentation, brace placement, or alignment.
Run gofmt -w . to format files in place. Many editors can run formatting automatically on save. A Golang project should normally commit formatted code only.
package main and func main().
gofmt keeps formatting consistent across projects.
Explore 500+ free tutorials across 20+ languages and frameworks.