Understanding Packages in Go

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS
Search for a command to run...

I am a self-taught Fullstack Web and Mobile App developer with over 4 years of experience. I have built a few apps using React and React Native with a backend in Nest.JS and Express.JS
No comments yet. Be the first to comment.
In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!
In this lesson, we’ll walk through how to write a simple function in Go, covering the basics of functions, writing a "Hello World" program, understanding comments, and best practices for naming things in Go. You’ll also have the opportunity to try ou...
In this lesson, we’ll explore how pointers work in Go, which is different from languages like Java and .NET. Go gives programmers control over memory allocation and layout, though it doesn’t support pointer arithmetic (e.g., adding to or subtracting ...

The time package offers the Time datatype, which represents an instant in time. It also includes several functions for extracting and formatting different components of a timestamp. For example, you can get the current time using time.Now(), and then...

In this guide, we'll explore how to work with strings and the strconv package in Go, covering useful functions that help manipulate and convert strings. Common String Operations Go provides several built-in functions in the strings package that allow...

In this lesson, we'll dive deep into the string datatype in Go. Strings are a sequence of UTF-8 characters. Go efficiently handles string memory by reserving only 1 byte for ASCII characters when possible, and between 2-4 bytes for UTF-8 characters w...

In Go, operators are symbols used to perform operations, such as arithmetic, logical, or bitwise operations. This lesson will cover different types of operators and their behaviour, along with examples and ASCII diagrams where needed. Arithmetic Ope...

In Go, packages are a key component of organizing and structuring code. Think of them like libraries, modules, or namespaces in other programming languages. Packages help you group related code together. When you build a Go program, it consists of one or more packages, and each Go file belongs to a single package.
However, one package can have multiple files. Even if a program is small, it's good practice to split the code across multiple files, all belonging to the same package. For example, if your file is part of the main package, the first line of your Go file should declare it like this:
package main
The main package is special because it represents the entry point of standalone applications in Go. Every Go application has exactly one main package and this is where execution starts.
When building a Go program, it's essential to compile packages in the correct order. Packages often rely on other packages, and this dependency determines the sequence in which they must be compiled. Each package is typically stored in its own directory, and if a package is updated, any program or package that depends on it must also be recompiled.
In Go, you use the import keyword to include functionality from other packages in your code. For example, to use the fmt package, which provides input-output functionality, you simply write:
import "fmt"
This tells Go to include the public functions, types, and other declarations from the fmt package. If you need to import multiple packages, there are a few ways to do it:
import "fmt"
import "os"
Or you can shorten it by using a grouped import statement:
import (
"fmt"
"os"
)
This method is more concise, especially when importing multiple packages, and is called "factoring."
Every Go package exposes certain elements (like functions, types, and variables) to other packages. However, Go enforces strict visibility rules based on the case of the identifier:
If an identifier starts with an uppercase letter (e.g., MyFunction), it is exported and can be accessed by other packages.
If an identifier starts with a lowercase letter (e.g., myFunction), it is only visible within the same package.
This is similar to the concept of public and private variables in object-oriented languages. The package name acts as a namespace, which allows different packages to have identifiers with the same name without conflicts.
For example, if two packages pack1 and pack2 both have a variable called Object, they can be accessed like this:
pack1.Object
pack2.Object
Sometimes, you might want to import a package with a shorter or more descriptive name. Go allows you to create an alias for an imported package. For instance, instead of always typing fmt, you could alias it to fm like this:
import fm "fmt"
Now, throughout your code, you'd refer to the fmt package using fm, like this:
fm.Println("Hello, Go!")
Go strictly follows the principle of "no unnecessary code." If you import a package but don't use it anywhere in your code, the Go compiler will throw an error. This rule ensures that your codebase stays clean and efficient.
Using these import techniques and following Go’s visibility rules, you can effectively manage dependencies and organize your code into clear, reusable components. Next, you'll learn how to define and use functions in Go.