Packages in Golang

All code in the Go language is organized into packages. Packages provide a convenient way to divide code into separate parts or modules. Modularity allows Define a package with the required functionality once and then use it many times in different programs.

The package code is located in one or more files with the go extension. The package keyword is used to define a package. For example:

package main
import "fmt"
 
func main() {     
    fmt.Println("welcome to go tutorials")
}

In this case, the package is called main. The package definition should come at the beginning of the file.

There are two types of packages: executable and reusable. To create executables, the package must be named main. All Other Packages are not executable. In this case, the main package must contain the main function, which is the entry point to the application.

Import packages

If there are already ready-made packages with the functionality we need that we want to use, then to use them, we can import them into the program by using the import statement. 
For example, in the example above, the functionality of displaying a message to the console using the Println function is used, which is defined in the FMT package. Accordingly, to use this feature, you need to import the fmt package (import "fmt").

It is not uncommon for programs to connect several external packages at once. In this case, you can import each batch sequentially:

package main

import "fmt"
import "math"
import "strings"
import "bytes"
 
func main() {
     
}

In this case, we import a bunch of packages, which contain functions that we can use in our program.

Alternatively, to shorten the definition of package imports, you can enclose all packages in parentheses:

package main

import (
 "fmt"
 "math"
 "strings"
 "bytes"
)
func main() {
         
}

In the same way, we can import both built-in packages and our own. A complete list of built-in packages in Go can be found at Golang packages.

Package of Several Files

A single package can consist of multiple files. For example, let's define two files in a folder:

main.go
test.go

In the test.go file, define a function for calculating the factorial:

package main

func Bar(a int) int {
    var c = 1
    for b:=1; b <= a; b++{
        c *= b
    }
    return c
}

This file belongs to the main package.

In the main.go file, we use the function to calculate the factorial:

package main
import "fmt"

func main() {
    fmt.Println("the factorial of number 3 = ", Bar(3))
    fmt.Println("the factorial of number 7 = ", Bar(7))
}

This file also belongs to the main package. There may be more files. Now let's compile the program from these files. To do this, go to the console to the project folder and run the command:

go build -o main main.go test.go 

The -o flag specifies what the name of the output file will be in this case, main. Then all the files are compiled. After completing this command will create a file main that we can run in the console:

> ./main

the factorial of number 3 =  6
the factorial of number 7 =  5040

Files in Different Packages

Now let's consider another situation when the files of our program are divided into different packages. Let's define a directory called calc in the project folder. Then add the following file test.go to the calc directory:

package calc

func Bar(a int) int {
    var c = 1
    for b:=1; b <= a; b++{
        c *= b
    }
    return c
}

The code for the test.go file belongs to the calc package. It's important to note that the feature name starts with a capital letter. As a result, this feature will be visible in other packages.

To use the Bar function, you need to import this package in the main.go file:

package main

import (
	"./calc"
	"fmt"
)

func main() {
	fmt.Println("the factorial of number 3 = ", calc.Bar(3))
	fmt.Println("the factorial of number 7 = ", calc.Bar(7))
}

The path "./calc" indicates that the package is in the calc folder.

Compilation and execution of the program are carried out in the same way as before, without the need to specify all files from other packages:

> build -o main main.go
> ./main
the factorial of number 3 =  6
the factorial of number 7 =  5040