Golang packages

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.

GO : Packages

What is the package in Golang?

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

How to declare a package in Golang?

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>

Types of packages in Go?

There are two package types in Golang

  1.   main packages (executable)
  2.   non-main packages (nonexecutable)  
    GO : Packages

Golang main() package

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

Golang fmt package

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.

GO : Packages
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

GO : Packages

In go language, each data type has a unique format specifier.

Format specifier Data Type
%s string
%d integer
%g float
%t bool

The program shows different print functions under fmt packages


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

math package in golang

 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

  1.   Abs function: Generates absolute value of a number
    
    package main
    import (
     "fmt"
     "math"
        )
    func main() {
     fmt.Println(math.Abs(-1.3))
    }
    
    

    Output:

    
    1.3
    
  2.   Trigonometric functions like sine, cos, tan, etc.
    
    package main
    import (
     "fmt"
     "math"
        )
    func main() {
     fmt.Println(math.Sin(3))     
                   fmt.Println(math.Cos(10)) 
       
    }
    
    

    Output:

    
    0.1411200080598672
    -0.8390715290764524
    
  3.   Log functions to calculate the logarithm 
    
    package main
    import (
     "fmt"
     "math"
        )
    func main() {
     fmt.Println(math.Log(9))
    }
    
    

    Output:

    
    2.1972245773362196
    
  4.   mod,ceil,floor functions
    
    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
    
  5.    Max and Min function
    
    package main
    import (
     "fmt"
     "math"
        )
    func main() {
     fmt.Println(math.Min(7, 5)) 
    fmt.Println(math.Max(7, 5)) 
    }
    
    

    Output:

    
    5
    

How to import packages in Golang?

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.

  1.   Multiple imports statement First way of importing packages to Golang
    
    // 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.

  2.   Factored import statements 
    
    // 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.

What is exported and unexported names 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.