What is a package
A package is a logical organization or a grouping of files. A package can contain multiple files that belong to it.
Consider a package as a folder on a computer which contains multiple files.
Following illustration will clarify the concept and architecture of a go package.
where mypackage is the name of package containing three go files.
Packages are created to avoid naming conflicts in an application.
If two different teams are working on an application, then they can create functions or variables with the same name by creating different packages since you can not have same names in different files in the same package.
A file in Go is declared to be belonging to a package using the
package
statement followed by the name of the package.package
statement should be the first statement in a go file. Example,
package game /* more code */
It is mandatory to write package
statement in a go file. This means that every go file should belong to a package.
If you omit the package
statement, then following error will be generated.
expected ‘package’, found ‘import’
assuming that 'import'
is the first statement in a file.
Package naming convention
Name of the package can be any user defined name confining to below rules.
- It must not start with a number or a special character.
- It should start with an alphabet. Starting with an underscore is allowed but it should not.
- Package name can not contain special characters, only alphabets, numbers and underscore is permitted.
- Lower case package names are recommended. All builtin packages of go are defined in lower case.
Importing package
It is possible to import other packages in a file using an import statement. An import statement begins with import
keyword followed by the name of package in quotes.
An example of import statement is import "fmt"
which is what we did while learning to write the first Go program, where fmt is the name of a package.
Importing multiple packages
Any number of packages can be imported in a single Go file. There are two syntax to import multiple packages.
1. Multiple import statements
Write an import statement for each package to import which means that there will be multiple import statements as well. Example,
import "fmt" import "os" import "mypackage"
2. Single import statement
Second and more used method of importing multiple packages is by enclosing the package names in parenthesis and a single import statement. Example,
import ( "fmt" "os" "mypackage" )
Don’t Miss these!
Following points should be remembered regarding go packages.
- All files in a folder should belong to the same package. This means that if there are 2 files A.go and B.go under folder test, then both the files should have the same package name.
- It is not necessary that the name of package should match the name of the folder. Thus, both the files can have mypackage as the package name with a different folder name.
- Two files in the same package can not have a function or a variable declared with the same name.
Thus, if A.go and B.go lie in package test, then they can not contain a function with the same name. Doing that would raise an error.print redeclared in this block
previous declaration at .\A.gowhere
print
is the name of the duplicate function. - You can not import same package more than 1 time in the same file.
fmt redeclared as imported package name
- If you import a package then you should use it else an error will raised.
imported and not used: “fmt”
Types of packages
There are two types of packages in go programming language.
Executable package
An executable package is one which can be run or executed. A go application can have only one executable package and it should be declared with the name main.
Also, the executable package should contain a file having main
function. In simple terms, a go file which is executed should belong to package main.
Library package
These are helper packages having files that contain reusable code. Library packages are never executed. All of standard go packages are Library packages only.