A package is a container that contains various functions to perform specific tasks. For example, the math package includes the Sqrt() function to perform the square root of a number.
While working on big projects, we have to deal with a large amount of code, and writing everything together in the same file will make our code look messy. Instead, we can separate our code into multiple files by keeping the related code together in packages.
Now, we can use the package whenever we need it in our projects. This way we can also reuse our code.
Remember this Hello World program, you wrote while starting with Go programming.
Here, we have started our program with the package main.
Every Go program starts with the main package. Whenever the compiler sees the main package, it treats the program as the executable code.
In our previous example, we have used the code
Here, we have used the import keyword to import the fmt package.
Once we import the package, we can use all of its functions in our program. For example,
In the above program, we have imported the fmt package in our program. Notice the code
Here, we are using the Println() function of the fmt package to print the text.
Now that we know how to import packages, let's learn about some of the popular packages:
fmt Packagemath Packagestring PackageIn Go, the fmt package provides functions to format our input/output data. For example, the fmt.Println() function prints the data to the output screen.
Some of the commonly used fmt functions:
| Functions | Descriptions |
|---|---|
Print() | prints the text to output screen |
Println() | prints the text to output with a new line character at the end |
Printf() | prints the formatted string to the output screen |
Scan() | get input values from the user |
Scanf() | get input values using the format specifier |
Scanln() | get input values until the new line is detected |
To use these functions, we must import the fmt package.
Output
In the above example, we have used the fmt.Scan() function to take input value and assign it to the number variable. We then print the value of number using the fmt.Println().
The Println() function adds a newline character at the end by default. That's why the next statement, fmt.Print() prints the text, Using Print in the new line.
However, Print() doesn't add the newline character by default, the next print statement prints the text Using Println in the same line
In the above example, functions
fmt.Scanf("%d", &number) - takes integer input value and assign it to the number variablefmt.Printf("%d", number) - replaces the %d format specifier by the value of number and prints itThe math package provides various functions to perform mathematical operations. For example, math.Sqrt() finds the square root of a number.
Some of the commonly used math functions:
| Functions | Descriptions |
|---|---|
| Sqrt() | returns the square root of the number |
| Cbrt() | returns the cube root of the number |
| Max() | returns the larger number between two |
| Min() | returns the smaller number between two |
| Mod() | computes the remainder after division |
To use these functions, we must import the math package.
Here, we have imported the math package in our program. This is why we are able to use math-based functions like Sqrt(), Max(), etc in our program.
In our example, you might have noticed that we have used two import statements to import the fmt and math packages. In such cases, we can import both packages together using a single import statement. For example,
The strings package provides functions to perform operations on UTF-8 encoded strings. For example, strings.Contains() checks if the string contains a substring.
Some of the commonly used strings functions:
| Functions | Descriptions |
|---|---|
| Compare() | checks if two strings are equal |
| Contains() | checks if the string contains a substring |
| Count() | counts the number of times a substring present in the string |
| Join() | creates a new string by concatenating elements of a string array |
| ToLower() | converts the string to lowercase |
| ToUpper() | converts the string to uppercase |
To use these functions, we must import the strings package.
Output
In the above example, we have used functions of the strings package to perform various operations on the strings.
So far, we have been using packages that are already defined inside the Go library. However, Go programming allows us to create our own custom packages and use them just like the predefined packages.
To create a custom package, we first need to create a new file and declare the package. For example,
Now, we can create functions inside the file. For example,
In the above example, we have created a custom package named calculator. Inside the package, we have defined two functions: Add() and Subtract().
This file doesn't contain the main package. Hence, the Go compiler doesn't consider this as an executable program and it is created for the sole purpose of sharing and reusing.
Now, we can import the custom package in our main file.
Here, we have successfully imported the calculator package in our program and used its functions.
We have used Packages/calculator as the name of the package. This is because the calculator package is present inside the Packages folder and we are providing the path to that package from the location of the mainfile.