Fundamentals 8 min read

Master Go Packages, Imports, Variables, and Constants in Minutes

This article explains Go's core concepts, covering package definitions and imports, variable declaration styles and scopes, as well as constant usage with iota, providing clear examples and code snippets to help developers understand and apply these fundamentals effectively.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Go Packages, Imports, Variables, and Constants in Minutes

Package Concept

A package is an essential part of Go; each source file starts with a package "package_name" declaration, which is used when importing the package.

Every Go file belongs to exactly one package, which may consist of many .go files; file names and package names can differ.

An executable program must have a single package named main, which serves as the entry point.

Packages can call each other to reduce code duplication.

Imported packages must be used, otherwise the compiler reports an error (unless the import name is prefixed with an underscore to ignore it).

Package Import

1. Import Methods

Method 1:

import "fmt"
import "os"

Method 2:

import "fmt"; import "os"

Method 3 (recommended):

import (
    "fmt"
    "os"
)

2. Importing Other Packages

Go resolves import paths relative to the GOPATH/src directory. If multiple GOPATH entries exist, Go searches them sequentially until the package is found; otherwise compilation fails.

Example:

package add
import "fmt"
func Sum(a int, b int) {
    var c int
    c = a + b
    fmt.Println("res", c)
}

In main:

package main
import (
    "fmt"
    "day02/eg1" // import package
)
func main() {
    add.Sum(2, 3) // call package function
    fmt.Println("Hello, World!")
}

Variables

1. Single Variable Declaration

All variables must be declared before use. Declaration and assignment can be separate or combined.

Separate declaration and assignment: var name type then name = expression Combined declaration and assignment: var name type = expression (shorthand: name := expression)

Example:

package main
import "fmt"
func main() {
    var a string
    a = "wd"
    var age int = 22
    fmt.Println(a, age)
}

2. Multiple Variable Declaration

Separate declaration then assignment: var v1, v2, v3 type and v1, v2 = val1, val2 Combined declaration and assignment: var v1, v2 = val1, val2 (shorthand: v1, v2 := val1, val2)

Using var() block for multiple variables.

Example:

package main
import "fmt"
func main() {
    var a, b int
    a, b = 1, 2
    var c, d = 3, "wd"
    e, f := 4, "hello"
    fmt.Println(a, b, c, d, e, f)
}

Using var() block:

package main
import "fmt"
func main() {
    var (
        a int   // default 0
        b string // default ""
        c = 3
    )
    fmt.Println(a, b, c)
}

3. Variable Naming Rules

First character may be any Unicode character or underscore.

Subsequent characters may be Unicode, underscore, or digits.

Keywords cannot be used as variable names.

break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var

4. Visibility

A variable is exported (visible outside its package) if its name starts with an uppercase letter; otherwise it is private.

5. Scope

Local variables are defined inside functions and are scoped to that function.

Global variables are defined outside functions, belong to the entire package, and are exported if their name starts with an uppercase letter.

Constants

Constants are immutable values known at compile time. Their types can be boolean, numeric (integer, float, complex), or string.

Definition syntax:

const Name Type = value

Multiple constants:

const (
    a = 0 // type inferred
    b = 1
    c = 2
)

iota is a special constant generator that increments automatically within a const block, useful for creating enumerations.

const (
    a = iota // 0
    b        // 1
    c        // 2
    d = "ha" // iota still increments
    e        // "ha"
    f = 100  // iota increments
    g        // 100
    h = iota // 7, resets count
    i        // 8
)

Example usage:

package main
import "fmt"
func main() {
    const (
        a = iota // 0
        b        // 1
        c        // 2
        d = "ha" // 3
        e        // 4
        f = 100  // 5
        g        // 100
        h = iota // 7
        i        // 8
    )
    fmt.Println(a, b, c, d, e, f, g, h, i)
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingGoPackagesConstantsiota
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.