How to import third-party packages in Golang?

To import third-party packages in Go, you can use the import keyword. Here are several common ways to import third-party packages.

  1. Import the full package path:
import "github.com/包名/包路径"

For instance, to import the github.com/gin-gonic/gin package, you can use the following statement:

import "github.com/gin-gonic/gin"
  1. Introduce with an alias:
import 别名 "github.com/包名/包路径"

For example, to import the github.com/gin-gonic/gin package and alias it as gin, you can use the following statement:

import gin "github.com/gin-gonic/gin"
  1. I have to finish my homework before going out with my friends.
import . "github.com/包名/包路径"

To introduce the “github.com/gin-gonic/gin” package and import all of its public functions, methods, types, and variables directly into the current file’s namespace, you can use the following statement:

import . "github.com/gin-gonic/gin"

Please note that using “import” may lead to naming conflicts, so use it carefully.

  1. I have to leave early tomorrow morning.
import _ "github.com/包名/包路径"

This method is typically used for cases where only the init function of the package is desired to be executed without using any other functions, methods, types, and variables in the package.

Here are several common methods to import third-party packages. Choose the one that suits your needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds