
In Go, packages are the fundamental units of organization for source code. A package is a collection of Go source files in a single directory.
A package is identified by the directory it resides in, not by the files it contains. This makes it easy to manage and reuse code.
To create a new package, you simply need to create a new directory and add a file named "main.go" to it. The directory name becomes the package name.
For example, if you create a directory named "math", the package name will be "math".
Broaden your view: Golang vs Go
Go Modules
A Go Module is a collection of Go packages, and it's the foundation for creating custom packages in Go. You need to understand Go Modules before creating custom packages.
To create a Go Module, you need to run the command `go mod init` followed by the name of the module, such as `learnpackage`. This will create a file named `go.mod` that contains the module's name and the version of Go being used.
Check this out: Golang Go
The module path is derived from the name of the Go Module, and it's used to import packages created within the module. The `go.mod` file also manages third-party packages and their versions.
Creating a Go Module is a straightforward process that involves running a single command. You can create a new module by running `go mod init learnpackage` in the directory where you want to create the module.
A module is essentially a tree of Go source files with a `go.mod` file in the root directory. To turn your code into a Go Module, you need to add a valid `go.mod` file to the directory.
The `go.mod` file generated by running `go mod init` declares the module path and the version of Go being used. This file will be revisited later when using third-party Go packages.
Worth a look: Golang Template Html
Function and Method Basics
In Go, functions and methods are the building blocks of your code. They're used to perform specific tasks, and you can think of them as reusable blocks of code.
Check this out: Golang Source
A function is a block of code that takes in arguments and returns a value. You can declare a function using the `func` keyword, followed by the function name and the input parameters in parentheses.
Functions are useful for encapsulating complex logic and making your code more modular. By breaking down your code into smaller, manageable functions, you can write more efficient and maintainable code.
You can call a function by its name, followed by parentheses containing the input arguments. For example, if you have a function `add(a, b int) int`, you can call it like this: `result := add(2, 3)`.
A unique perspective: Golang Reflect to Call Function in Package
Exported and Local Functions/Methods
In Go, the way you name your functions and methods determines whether they're exported or local. A local function is named in full lowercase, and it can only be accessed within the same package.
The difference between exported and local functions is subtle, but it's crucial to understand. In the example of the math package, we have two functions named identically, but one starts with a capital letter, Multiply. This is the distinction between a local and an exported function in Go.
See what others are reading: Golang Func Type

Exported functions start with a capital letter and can be accessed from within and outside the package where they were defined. On the other hand, local functions are named in full lowercase and can only be accessed within the same package.
If we published our package today, we can import this math package and access Multiply, but attempting to access multiply will throw an error. This is because multiply is a local function and cannot be accessed outside of the package.
Here's a quick summary of the difference between exported and local functions:
- Exported functions start with a capital letter and can be accessed from anywhere.
- Local functions are named in full lowercase and can only be accessed within the same package.
This concept is essential to creating modularized pieces of code that can be shared across projects or with other developers. By understanding the difference between exported and local functions, you can write beautifully structured Go code that's easy to maintain and extend.
Blank Identifier
The blank identifier is a lifesaver when you're importing packages you haven't used yet, but plan to use later.
It's illegal in Go to import a package and not use it anywhere in the code, which can slow down compilation time.
The compiler will complain if you do so, which can be frustrating, especially during active development.
Using the _ blank identifier allows you to import packages without using them, avoiding this issue.
This can be a huge time-saver, especially when you're working on a project and know you'll need certain packages later on.
The _ blank identifier is a simple but effective solution to this common problem.
Consider reading: Gcloud Api Using Golang
String Example
Strings are a fundamental part of programming, and knowing how to work with them is crucial.
You can use functions from the strings package to perform various operations on strings, as seen in the example where we used the strings package to manipulate a string.
Working with strings can be as simple as concatenating two strings together, or as complex as using regular expressions to extract specific information.

In the example, we used the strings package to perform operations on a string, which is a great way to get started with string manipulation.
The strings package provides a range of useful functions, including the ability to check if a string is empty or not, as seen in the example.
Understanding how to use the strings package can save you a lot of time and effort in your programming projects.
For instance, you can use the strings package to remove leading or trailing whitespace from a string, which can be useful when working with user input.
For another approach, see: Golang Use Cases
Importing in Go
Importing in Go is a straightforward process. You can import your own packages or third-party packages using the import keyword.
To use a custom package, you must import it first. The import path is the name of the go module concatenated by the directory where the package resides and the package name. For example, to import the simpleinterest package, you would use import "learnpackage/simpleinterest".
Packages in the standard library don’t need the module name prefix and hence fmt works without the module prefix. You can import the fmt package using the import keyword, for example, import "fmt".
Individual .go files can import and use exported types, constants, variables, and functions from other packages, including the packages in the Go standard library. The complete tree of Go standard library packages is available here.
When importing a package from the standard library, you need to use the full path to the package in the standard library tree, not just the name of the package. For example, to import the math/rand package, you would use import "math/rand".
You can also import third-party packages using their module path. For example, to import the github.com/fatih/color package, you would use import "github.com/fatih/color".
See what others are reading: Simple Http Server Golang Github
Function and Method Usage
In Go, functions and methods are the building blocks of your program's logic. They are reusable blocks of code that can be called multiple times from different parts of your program.
A function is a block of code that performs a specific task and can be called multiple times from different parts of your program. You can pass arguments to a function and return a value.
Go has a built-in function called `fmt.Println()` that prints its argument to the console. This function is often used for debugging and testing purposes.
Methods are functions that are attached to a specific type, such as a struct. They can be used to implement behavior that is specific to that type. For example, the `fmt.Printf()` function is a method that is attached to the `fmt` type.
You can define your own methods by using the `func` keyword followed by the receiver type and the method name. The receiver type is the type that the method is attached to. For example, the `fmt.Printf()` method has a receiver type of `*fmt.Formatter`.
You can also define a method on an existing type by using the `func` keyword followed by the existing type and the method name. For example, the `fmt.Println()` function can be used as a method on the `fmt` type.
Go Standard Library
The Go standard library is a treasure trove of pre-written code that you can use in your own projects. It's available here.
You can import and use standard library packages in your Go code, including the math/rand and fmt packages, which we've used to generate a random number and print a message.
To import a package from the standard library, you need to use the full path to the package in the standard library tree, not just the name of the package. For example, you'd use "math/rand" instead of just "rand".
Once imported, the package name becomes an accessor for the contents of that package, making it easy to use functions like rand.Intn() and fmt.Printf().
Importing Standard Library
Importing standard library packages is a fundamental aspect of Go programming. You can import and use exported types, constants, variables, and functions from other packages, including the Go standard library.
The complete tree of Go standard library packages is available online. You can access it by visiting the provided link.
To import a package from the standard library, you need to use the full path to the package in the standard library tree, not just the name of the package. For example, to import the math/rand package, you would use the full path "math/rand".
Once imported, the package name becomes an accessor for the contents of that package. You can use the functions from the package by calling them with the package name as a prefix. For example, to use the Intn() function from math/rand, you would call rand.Intn().
You can also import multiple packages at once using a single import statement. For example, you can import both the fmt and math packages together like this: "fmt math".
Some commonly used standard library packages include the fmt package, which provides functions for formatting input/output data, and the math package, which provides functions for mathematical operations.
Here are some commonly used functions from the fmt package:
Similarly, some commonly used functions from the math package include:
What Are They Used For?
Go's standard library is a treasure trove of useful functions and packages that make coding in Go a breeze. One of the most important aspects of the Go standard library is its ability to help us organize our code for better reusability and readability.
Packages are a collection of Go source code files that reside in the same directory, making it easy to maintain Go projects. We can create packages by functionality, such as simple interest calculation, compound interest calculation, and loan repayment calculation.
The strings package is another essential part of the Go standard library, providing functions to perform operations on UTF-8 encoded strings. This package includes functions like Compare(), Contains(), Count(), Join(), ToLower(), and ToUpper().
The strings package is incredibly useful for tasks like checking if a string contains a substring, converting strings to lowercase or uppercase, and more. For example, strings.Contains() checks if the string contains a substring, while strings.Join() creates a new string by concatenating elements of a string array.
Discover more: Golang String to Time
Here are some of the key functions provided by the strings package:
By using packages like simpleinterest and loanrepayment, we can reuse code and make our projects more maintainable.
Go Project Management
Go Project Management is a crucial aspect of working with Go, and understanding how it works can save you a lot of headaches down the line.
Historically, all Go code was stored in one giant monorepo, but Go Modules has changed that approach, allowing you to keep your projects under a separate directory.
You're no longer required to keep all your projects under $GOPATH, but all your downloaded dependencies are still placed under $GOPATH/pkg/mod, which can become an issue when using Docker containers.
This is because dependencies are stored outside of the project path, making them invisible from your IDE by default.
Additional reading: Golang Install Dependencies
Per-project dependency management
Go Modules allows for per-project dependency management, which is a departure from the traditional monorepo approach. You can store your Go code in separate projects, and each project has its own go.mod file.
With Go Modules, all your downloaded dependencies are stored under $GOPATH/pkg/mod. However, this can become an issue when using Docker containers for local development, as dependencies are stored outside of the project path.
You can use the GOFLAGS=-mod=vendor environment variable to opt-in to vendoring, but this is not recommended. Instead, you can use the vendor folder approach, which involves copying your project dependencies to a vendor folder.
Here's a step-by-step guide to vendoring your dependencies:
1. Install a dependency with go get.
2. Import it somewhere in your code.
3. Run go mod vendor to vendor your dependencies anew.
Alternatively, you can override GOPATH to manage dependencies per project. This approach is cleaner and doesn't compromise go get functionality.
By using Go Modules, you can manage dependencies on a per-project basis, making it easier to maintain and update your code.
Init Function
In Go, the init function is a special function that runs before the main function, allowing for initialization code to be executed.

It's a great way to set up dependencies and variables before the program starts. The init function is called immediately after the import statement, making it an ideal place to initialize variables that are used throughout the program.
The init function can be defined in a package, and it's a good practice to keep it simple and focused on initialization. For example, you can use it to initialize a database connection or load configuration files.
In Go, the init function can be used to run code before the main function, but it's not a replacement for the main function. The main function is still necessary for the program to start.
The init function can be defined in multiple files, but it's not a good idea to use it for complex logic or business rules. Keep it simple and focused on initialization, and you'll be fine.
In the "Project Structure" section, we discussed the importance of keeping the project organized and structured. The init function is a great way to implement this structure by initializing variables and dependencies in a clear and concise manner.
The init function is a powerful tool in Go, and it's worth learning how to use it effectively. By keeping it simple and focused on initialization, you can write more efficient and maintainable code.
Broaden your view: Golang Initialize Map
Lang Fmt

The fmt package in Go provides functions to format our input/output data. It's a crucial part of any Go project, especially when it comes to handling user input and output.
To use the fmt package, we must import it into our code. This is a simple step that sets us up for success.
The fmt package offers several functions for printing text to the output screen. These include Print(), Println(), Printf(), Scan(), Scanf(), and Scanln(). Each function has a specific use case, but they all help us communicate with the user.
Here are some of the most commonly used fmt functions:
In practice, Println() is often used to print messages to the user, while Scan() is used to get input values from the user.
Set PATH in Project Directory
Setting the PATH in your project directory can be a game-changer for Go project management. This approach involves overriding the GOPATH to point to the project directory, making packages accessible from the host.
Intriguing read: Package Django Project

Running Go from a container is a popular choice, and setting GOPATH on a project level is a feature that many IDEs should include. This allows you to manage your project's dependencies and packages more efficiently.
One of the downsides of this approach is the lack of interoperability with the Go runtime on the host machine. This means you'll need to commit to running all Go commands from inside the container.
Go App Structure
When building a Go web application, it's essential to understand how to structure your code. A typical Go application consists of two or more .go files, and both files are part of the main package because they both start with a package main statement.
You can break down your Go web application into sections, such as how to structure your code, manage dependencies, create dynamic database-driven pages, and authenticate and authorize users securely. A tutorial on this topic is available, which covers topics like packages, import statements, and modules in detail.
For your interest: Golang Applications
Here are the key sections of a Go web application structure:
- Packages
- The main package
- Importing and using standard library packages
- Unused and missing imports
- Exported vs unexported
- Modules
- Using multiple packages in your code
- Importing and using third-party packages
- Organizing import statements
In a typical Go application, the main function is the entry point for execution and resides in the main package. This function is essential for any executable Go application.
Consider reading: Golang Test Main
How to structure a web app?
Structuring a web app in Go can be a bit overwhelming, especially for beginners. You don't have to start from scratch, though. A good place to begin is by understanding the basics of Go's package structure.
The main package is the entry point of your Go application, and it's where you'll typically put your main function. You can have multiple main packages in a single module, which is useful for larger projects with multiple tools or applications.
For a simple web app, you might start with just two .go files, both part of the main package. This is a great way to get started, but as your project grows, you'll want to organize your code more effectively.
You might like: Golang App Development
You can move your main package files into a new directory, like cmd/cli, and your other package files into a separate directory, like internal/random. This helps keep your code organized and makes it easier to manage dependencies.
When importing packages, you can import them from anywhere within your module, not just from the root directory. This gives you a lot of flexibility in how you structure your code.
Here's a simple example of how you can structure your web app:
Remember, this is just a starting point, and you can adjust your structure as needed to fit your project's requirements.
Custom
Custom packages in Go allow us to create our own reusable code. We can create a custom package by declaring the package in a new file. For example, we can create a file and declare it as a package named calculator.
Inside the custom package, we can define functions. For instance, we can create functions like Add() and Subtract(). Note that this file doesn't contain the main package, making it non-executable. We create it solely for sharing and reusing code.
To use a custom package, we need to import it in our main file. We can do this by specifying the package name, which can be lengthy. To make it easier, we can use aliasing and assign a shorter name to the package. For example, we can use the alias str for the strings package.
Using aliasing can simplify our code and make it more readable. For instance, we can use the ToUpper() and ToLower() functions using the alias str. This approach helps us to write more efficient and organized code.
Main Function
The main function is the entry point for execution in every executable Go application. It's where the program starts running.
In Go, the main function should reside in the main package. This is a special package name that indicates the package contains code for an executable application.
The main function can be found in a file with the filename main.go. This is a conventional naming convention that makes the application entry point easy to find.
Any package with the name main must contain a main() function somewhere in the package. If it doesn't, you'll get an error when trying to run it.
Go Function and Method Details
Go functions are declared with the func keyword, followed by the function name and a list of arguments enclosed in parentheses. The function name should be a valid Go identifier.
A function can also have a return type specified, which is the type of value the function returns. For example, a function that returns an integer would have a return type of int.
Go methods are functions that are attached to a specific type. They are declared with the func keyword, followed by the method name and a receiver argument of the type that the method is attached to. The receiver argument is used to refer to the value on which the method is being called.
Methods can also have a return type specified, just like functions. For example, a method that returns an error would have a return type of error.
Go functions and methods can be recursive, meaning they can call themselves. This is useful for solving problems that have a recursive structure. However, recursive functions can also cause a stack overflow if they call themselves too many times.
Go's type system is designed to prevent recursive functions from causing a stack overflow by default. However, this can also make it difficult to write recursive functions that work correctly in all cases.
Additional reading: Golang Create Error
Frequently Asked Questions
Is Golang still popular in 2025?
According to the 2025 Developer Survey, Go (Golang) remains a popular choice among developers worldwide, preferred by 13.5% of global developers and 14.4% of professionals. Its strong position indicates a sustained interest in the language, despite a not-too-easy learning curve.
Featured Images: pexels.com

