Packages in Go

Kishore M G
4 min readOct 25, 2023

--

Hi there 👋, Welcome back. In this blog, I am going to share a main concept used in Go which stands as a main pillar to Golang.

What is a package?

A package is a container that holds go source files which is used to perform some specific task. In simple words, A package is a folder that contains multiple functions which is used to perform some intended operations like a add() function that returns sum of two numbers.

Packages in Golang

Let me explain the concept of package with an example:

Consider, a basic package “math” in golang. The “math” package contains multiple functions which performs specific operations.

Few functions defined in “math” package are

For more reference on “math” package, please do refer:

This Sqrt() function helps us to perform math operation that returns square root of the input given as a parameter.

package main
import (
"fmt"
"math"
)
func main() {
input := 5

result := math.Sqrt(input*input)

fmt.Printf("%.1f", result)
}

Output:

5.0

Importance of packages:

  1. Code Reusability:
    Use of packages improves code reusability.
    — Assume, the sqrt() function which we discussed earlier, returns the square root of input value.
    — So, Instead of writing the code from scratch for these operations which again increases the time and code replication, we combine all these function in a package and import it whenever we require. This improves the code reusability and also save the time of developers.
  2. Organization:
    — Packages helps us to organize our codebase. This improves our codebase structure and make our code look neat and clean.
    — Also, it helps to group related codes and combine those codes in a separate package which can used in our codebase at different places.
  3. Standard Library:
    — The standard library is a collection of wide range of functions which are well tested and provides functional usage in multiple areas. Let me tell you few areas that are covered by standard library: I/O, cryptography, http,…
  4. Encapsulation:
    — In golang, we used to export variables, functions using naming conventions.
    — When a variable name starts with an uppercase letter, then the variable/function is being exported. When a variable name starts with lowercase letter, then this restricts the variable/function from being exported.
    — This avoids interferences within packages and other packages can only use the exported variables or functions.

Types of packages:

  1. Executable package
  2. Reusable package

Executable package:

The package that we build and compile it into an executable program is called as Executable package. When a package is made as executable, it means that the program execution or Go application starts from this package.

Example:

package main
import "fmt"func main() {
fmt.Println("Hello, World!")
}

How to make a package as Executable?
To make a package executable, two things need to done:

  1. Declare the package name as main in order to make the package as Executable.
  2. The package must contain func main() because the main() function is an entry point for an executable package. When a program is made to run, the entry point for that program will be main() function.

How to run the Executable package?
Once the package is build and compiled, a binary compiled file will be generated. On the execution of that compiled binary file, the program starts to run.

Reusable package:

Reusable package is designed to perform some specific task which can be used in other packages instead of running the package itself. On building this package, no executable file will be generated. This mainly serves the purpose of reusability. All the standard library are referred as Reusable package.

Example:

// Reusable package
package mylogic
import "fmt"func Add(a int, b int) int {
return a + b
}
func Subtract(a int, b int) int {
return a - b
}
// Executable package which uses reusable package.
package main
import (
"fmt"
"mylogic"
)
func main() {
a := 10
b := 5

resultAdd := mylogic.Add(a,b)
fmt.Println("Addition result:", resultAdd)

resultSub := mylogic.Subtract(a,b)
fmt.Println("Subtraction result:", resultSub)
}

Output:

Addition result: 15
Subtraction result: 5

Goodbye, and thank you for reading! Hope, the information is useful. If you have more questions in the future, please feel free to add your questions in the comment section. Have a great day😃 !

Quote #2:

Learning never exhausts the mind. — Leonardo da Vinci

--

--

No responses yet