How to import a single package in Go or Golang?

July 19, 2022 - 1 min read

To import a package in Go or Golang, we can use the import keyword followed by the name of the package. The package imports should be at the top of the file but after the package name.

For example, let's say we need to get the current time. To do that we need the time package and then we need to use the Now() method to get the current time.

To use the time package we have to import it first.

It can be done like this,

package main

// we are importing the `time` package here
import "time"

func main(){
    // to get the current time we can use the
    // `Now()` method from the `time` package

We have successfully imported a single package in the Go language. Yay 🥳!

See the above code live in The Go Playground.

That's all 😃!

Feel free to share if you found this useful 😃.