Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Go package is a fundamental unit of code organization, compilation, and encapsulation, consisting of one or more .go source files residing in the same directory. All files within a package share the same namespace and are compiled together as a single entity, allowing internal declarations to be mutually accessible regardless of the specific file in which they are defined.

Package Declaration

Every Go source file must declare its package membership as the first non-comment line of code.
package mathutils
By convention, the package name is a short, lowercase word that matches the base name of the directory containing the files. All .go files within a single directory must declare the exact same package name, with the strict exception of test files (_test.go), which may optionally use a _test suffix (e.g., mathutils_test).

Visibility and Export Rules

Go does not use access modifier keywords (like public or private). Instead, it relies on lexical casing to determine the visibility of identifiers (variables, constants, functions, types, and struct fields) across package boundaries.
  • Exported (Public): Identifiers beginning with an uppercase letter are accessible to external packages that import them.
  • Unexported (Private): Identifiers beginning with a lowercase letter are strictly scoped to the declaring package.
package config

// Exported: Accessible outside the 'config' package
var MaxConnections int = 100
func Load() {}
type Server struct {
    Host string // Exported field
    port int    // Unexported field
}

// Unexported: Accessible only within the 'config' package
var defaultTimeout int = 30
func parse() {}

The main Package

The main package is a special designation that instructs the Go compiler to build an executable binary rather than a shared library archive. A main package must contain a main() function, which the Go runtime invokes as the entry point of the application.
package main

import "os"

func main() {
    os.Exit(0)
}

Importing Packages

To consume exported identifiers from another package, a file must declare an import block. The string provided is the import path, which is a globally unique identifier derived from the project’s go.mod file and the directory structure.
import (
    "fmt"                               // Standard library package
    "github.com/organization/repo/auth" // Third-party or internal module package
)
Import Modifiers: Go provides specific syntax to modify how a package is imported into the current file’s namespace:
import (
    // Alias: Binds the package to 'log' instead of 'logrus'
    log "github.com/sirupsen/logrus" 
    
    // Dot import: Pulls all exported identifiers directly into the local namespace
    . "math" 
    
    // Blank import: Compiles the package to trigger its init() functions, 
    // but ignores its identifiers to bypass the "unused import" compiler error
    _ "github.com/lib/pq" 
)

Package Initialization

A package can define one or more init() functions to perform setup tasks before the program executes. The Go runtime guarantees that init() functions are called sequentially after all package-level variable declarations are evaluated, and strictly before the main() function begins.
package database

var connectionString string

func init() {
    connectionString = "postgres://localhost:5432"
}
If a package imports other packages, the imported packages are initialized first. A single package can contain multiple init() functions across its files, which are executed in lexical file name order.
Master Go with Deep Grasping Methodology!Learn More