Which keyword is used to import the packages in Go language?

In this tutorial, we are going to learn about the keyword and various ways to import the package with syntax and examples in Golang.
Submitted by IncludeHelp, on October 01, 2021

The import keyword is used to import the packages inside our Go language program, or it is also used for importing the package into other packages.

Consider the below syntaxes:

If there is one package, we can import it in these two ways,

import "package_name"
import (
    "package_name"
)

If there are multiple packages to be imported, we can import them in these two ways,

import "package_name_1"
import "package_name_2"
...
import (
    "package_name_1"
    "package_name_2"
    ...
)

Consider the below example – In this example, we are importing two packages.

// The main package
package main

import (
	"fmt"
	"time"
)

// The main function
func main() {
	fmt.Println("Hello, world!")
	fmt.Println("The time is", time.Now())
}

Output:

Hello, world!
The time is 2009-11-10 23:00:00 +0000 UTC m=+0.000000001

An example of importing packages from github.com/eliben/modlib/clientlib

// The main package
package main

// Importing packages
import "fmt"
import "github.com/eliben/modlib"
import "github.com/eliben/modlib/clientlib"

// The main() function
func main() {
	fmt.Println(modlib.Config())
	fmt.Println(clientlib.Hello())
}

Output:

modlib config
clientlib hello

Golang FAQ »




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.