HomeAuthorsContact
GO and Package importation

GO and Package importation

By Frédéric Schmidt
June 09, 2020
2 min read

As we saw in the previous post regarding the definition of packages, this concept is very important in application design and as part of good practice. We also talk about the principle DRY - Don’t Repeat Yourself, but we will talk about this principle later.

Generally speaking, you must be able to reuse our packages but also those provided in the base library or any other third-party libraries.

Example

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, playground")
}

In our example, we ask GO to import the _fmt_package, This package contains a set of functions related to display and formatting on the screen. Thanks to this package approach it will allow reuse in any other program, application or package. In addition, to access a function contained in this package, we will use point notation. We’ll prefix with the package name and then the dot and finish the function name in our case.

Benefits

  • When compiling, only the parts of code that have evolved will be recompiled, which saves time on large applications;
  • The creation of a package allows to have a better organization of its code and to be made easier to find functions or other parts of code. One can imagine having a package that deals only with HTTP requests and another with FTP requests. We could also reason in functional areas.
  • Thanks to the access to the package functions by the point notation, we avoid having name conflicts on the functions. The writing will be clear, concise and precise.

Import

Definition

This import declaration allows the use of functions that are in packages outside our application directly within our source code.

Keyword

import

Syntax(s)

There are several syntaxes for importing packages. See below for the different writes.

Note(s)

  • Do not use spaces in package names and package aliases.
  • GO does not allow circular references in packages.

Scope

The scope of the import of a package is at the file level and not at the scope of the package. If it is used on several files, it should be imported into each GO file.

Syntaxes

Presentation of the different entries to import packages into a program. The names of packages are always in quotation marks. For further clarification, I will refer you to the official documentation on language specifications here

Simple

package main

import "fmt"

func main() {

    fmt.Println("Foobar")

}

Multiple

package main

import (
    "fmt"
    "math"
)

func main() {

    fmt.Println(math.Mod(7, 2))

}

Using Alias

package main

import (
    "fmt"
    myMathPackage "math"
)

func main() {

    fmt.Println(myMathPackage.Mod(7, 2))

}

No package name

This notation is not recommended, even to be banned because it will rather bring confusion in the reading of the code.

package main

import (
    . "fmt"
    . "math"
)

func main() {
    Println(Mod(7, 2))
}

Import with the “blank identifier”

This will allow you to have the initializations that are, possibly, in the math package. All these initializations must be in the init function of the package. Not everything else will be accessible.

package main

import (
    "fmt"
    _ "math"
)

func main() {
    fmt.Println("My name is John Doe")
}

Sub-directory

In some cases, packages or even yours may be subdivided into other sub-adjacent packages. So you have to be able to import them as well. Let’s take the case of the math_package, it is subdivided into 4 packages (so 4 subdirectories) which are: _rand, big, cmplx, bits and take for example the case where you want to work only with bits.

package main

import (
    "fmt"
    "math/bits"
)

func main() {
    fmt.Printf("LeadingZeros16(%016b) = %d\n", 1, bits.LeadingZeros16(1))
}

Tags

#ProgramingLanguages#go#goland#package#importation
Frédéric Schmidt

Frédéric Schmidt

Software Architect

I'm Software Developer & Architect. I'm more than 20 years experiences in differents IT service companies and software editors. I like studies some new technologies and share this passion altroughts my technical blog.

Expertise

Architecture
Development
Modelisation
Technical writer

Social Media

githublinkedin

Related Posts

GO and Packages
Programming Languages
GO and Packages
June 09, 2020
1 min
© 2022, All Rights Reserved.

Quick Links

Contact Us

About me

Social Media