Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Golang Syntax: Packages, Imports, main Function and Formatting

How a Golang File Is Structured

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.

Basic Program Structure
package main

import "fmt"

func main() {
    fmt.Println("Hello from Golang")
}

Package Names and Visibility

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.

NameVisible Outside Package?Typical Use
UserYesPublic model type
NewUserYesConstructor-style function
validateEmailNoInternal helper
dbNoPackage-private variable

Imports

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.

Grouped Imports
package main

import (
    "fmt"
    "strings"
    "time"
)

func main() {
    title := strings.ToUpper("golang syntax")
    fmt.Println(title)
    fmt.Println("Generated at:", time.Now())
}

Functions and Statements

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.

Function Syntax
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)
}

Comments and Documentation

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.

Documentation Comment
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
}

Formatting with gofmt

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.

  • Use tabs for indentation after formatting; let gofmt handle it.
  • Keep braces on the same line as declarations and control statements.
  • Remove unused imports and variables instead of leaving them in place.
  • Use short comments for non-obvious decisions, not for every line.
Key Takeaways
  • Every Golang file starts with a package declaration.
  • Executable programs use package main and func main().
  • Capitalized identifiers are exported from a package.
  • Unused imports and variables are compile errors.
  • gofmt keeps formatting consistent across projects.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.