DEV Community

Cover image for How to import a single package in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to import a single package in Go or Golang?

#go

Originally posted here!

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
    time.Now()
}
Enter fullscreen mode Exit fullscreen mode

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 πŸ˜ƒ.


Top comments (0)