In this tutorial, you will learn about packages in Golang. The Go programming languages use different packages which we discuss in detail together with how to import them to your program how to write a package etc.
In go programming language, programs are organized into packages.
A package can be defined as a group of source files in the same directory which are compiled together. Variables, types, functions, methods, constants which are defined in one source file are visible to other source files under the same package.
In a simple way, the package can be defined as a directory with many source files that reside in the Go workspace. Each go program belongs to certain packages. Every program has a common package i.e. main package.
Note: A package is composed of multiple files and the multiple files together form a package
A package is declared by a keyword package followed by a package name. The syntax used to declare a package is as shown below.
Example: package main
package <package_name>
There are two package types in Golang
Every go programming language begins its code with package main. The first line in the go source code is the declaration of the package for the main function. The source code starts its execution from the main function, main() and execution will be successful only if the package responsible for execution is present in the source file i.e. the package main.
package main //package declaration
import "fmt"
func main() { // program starts execution
fmt.Println("Hello world ")
} //program terminates execution
Output:
Hello world
The fmt package in Golang includes different functions for formatting and printing output to the console. The fmt package includes the very commonly used formats in all source files like Println, Printf, and Print.
Printf | Println | |
---|---|---|
fmt.Print() | fmt.Println() | fmt.Printf() |
Prints the string or contents inside the parentheses (). | Printf has a similar function to Print together it prints a new line by default. | Prints formatted string to the console. |
Eg fmt.Print(“golang”) | Eg fmt.Println(“golang”) | Eg fmt.Printf(“%s”,golang) the %s replaces with golang value |
In golang different formats, specifiers are used based on their data type such as %s, %d, etc. In below-given program
In go language, each data type has a unique format specifier.
Format specifier | Data Type |
---|---|
%s | string |
%d | integer |
%g | float |
%t | bool |
package main
import "fmt"
func main() {
site := "Learn eTutorials"
fmt.Print("golang")
fmt.Println("golang")
fmt.Println("-----------------------")
fmt.Println("golang")
fmt.Print("golang")
fmt.Println("")
fmt.Printf("%s",site)
}
Output:
golanggolang ----------------------- golang golang Learn eTutorials
In go programming language math package is composed of all mathematical functions.
The syntax used to import a mathematical function to a source code is
import “math”
Or
import (
“math”
)
The math package is available for different mathematical calculations like
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Abs(-1.3))
}
Output:
1.3
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Sin(3))
fmt.Println(math.Cos(10))
}
Output:
0.1411200080598672 -0.8390715290764524
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Log(9))
}
Output:
2.1972245773362196
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Mod(14, 5)) // mod return remainder after div
fmt.Println(math.Ceil(45.5)) // rounds to up
fmt.Println(math.Floor(39.7)) // rounds to down
}
Output:
4 46 39
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Min(7, 5))
fmt.Println(math.Max(7, 5))
}
Output:
5
In go programming languages there are two ways of importing packages to a Go source file. In our previous tutorials, we have used a mix of these two types of importing packages.
// Multiple import statements
import "fmt"
import "time"
import "math"
import "math/rand"
In multiple import statements, the packages are imported with the keyword import followed by the package name which is enclosed within double-quotes.
// Factored import statements
import (
"fmt"
"time"
"math"
"math/rand"
)
In factored import statement the packages that need to be imported are enclosed within parentheses ( ) which in turn begins with keyword import. Here fmt, time, math, rand are different types of packages available in Golang.
Exported names are visible outside the package and begin with capital letters,
Unexported names are keywords that are visible inside the package are not exported and do not begin with a capital letter.
package main //package
import ( // Factored import statements
"fmt"
"math"
)
func main() {
// MaxInt64 is an exported name
fmt.Println("Max value of int64: ", int64(math.MaxInt64))
// Phi is an exported name
fmt.Println("Value of Phi (?): ", math.Phi)
// pi starts with a small letter, so it is not exported
fmt.Println("Value of Pi (????): ", math.pi)
}
Output:
_/home/Tmkxll ./prog.go:16:38: cannot refer to unexported name math.pi ./prog.go:16:38: undefined: math.pi
Note: The error can be solved by using math. Pi
When it is resolved resulting output is as shown below
Output:
Max value of int64: 9223372036854775807 Value of Phi (?): 1.618033988749895 Value of Pi (????): 3.141592653589793
In the next tutorials, we will discuss some more packages available in golang like time, rand, etc in detail.