What should I do if golang cannot import packages?
Solution to the problem that golang cannot import packages:
Import packages:
Standard package use is a given short path, such as "fmt", "net/http"
For your own package, you need to specify a directory under the working directory (GOPATH). The import package is actually based on the working directory. Folder directory
Multiple ways to import packages:
Import directly from the
$GOPATH/src
directoryimport "test/lib"
(The path is actually$GOPATH/src/test/lib)
Alias import:
import alias_name "test/lib"
, when used in this way, you can directly use the aliasUse the dot to import:
import. "test/lib"
, function When using it, simply omit the package name
and use underscores to import: import _ "test/lib". This operation actually just introduces the package. When a package is imported, all its init() functions will be executed, but sometimes you don't really need to use these packages, you just want its init() function to be executed. At this time, you can use the _ operation to reference the package. Even if you use the _ operation to reference a package, you cannot call the exported functions in the package through the package name, but just to simply call its init function ().
Often these init functions register the engines in their own packages so that they can be easily used by the outside world. For example, packages that implement database/sql all call sql.Register(name) in the init functions. string, driver driver.Driver)
Register yourself, and then it can be used externally.
Relative path importimport "./model"//
The model directory in the same directory as the current file, but this method of import is not recommended
Package import Process description:
The initialization and execution of the program start from the main package. If the main package also imports other packages, they will be imported in sequence during compilation. Sometimes a package will be imported by multiple packages at the same time, so it will only be imported once (for example, many packages may use the fmt package, but it will only be imported once, because there is no need to import it multiple times).
When a package is imported, if the package also imports other packages, the other packages will be imported first, and then the package-level constants and variables in these packages will be initialized, and then init will be executed. function (if any), and so on. After all imported packages are loaded, the package-level constants and variables in the main package will be initialized, then the init function in the main package will be executed (if it exists), and finally the main function will be executed. The following figure explains the entire execution process in detail:
Note:
There are several ways to import Go packages, with different uses. The code is stored uniformly in the working directory. There will be many packages in the working directory. Different packages are organized by directory, and the packages are composed of multiple code files. When importing a package, import it according to the unique path of the package. The imported package must be used by default. If it is not used, the compilation will fail and needs to be removed to reduce the introduction of unnecessary code. Of course, there are other usage scenarios. By default, we use the file name as the package name for easy understanding. Different packages organize different function implementations to facilitate understanding.
Did you use the package source code or .a when compiling?
A non-main package will generate a .a
file after compilation (generated in a temporary directory, unless it is installed to $GOROOT
or # using go install ##$GOPATH, otherwise you won’t be able to see it. a), used for subsequent executable program linking. For example, the source code path corresponding to the package in the Go standard library is:
$GOROOT/src, and the path to the compiled .a file of the package in the standard library is
$GOROOT/pkg/darwin_amd64 Down. A strange question arises in my head. When compiling, does the compiler use .a or source code?
- When using third-party packages, when the source code and .a are both installed, the compiler links to the source code. The so-called use of third-party package source code is actually a link to the
.a
file in the temporary directory compiled with the latest source code.
- Are the packages in the Go standard library also like this? For standard libraries, such as fmt, when compiling, should you use the source code under $GOROOT/src or the compiled .a under $GOROOT/pkg? However, unlike custom packages, even if you modify the source code of the fmt package (without recompiling the GO installation package), when the user source code is compiled, there will be no attempt to recompile the fmt package. The link will still be compiled when the link is made. fmt.a
In Go language, is the last element in the path after import the package name or the path name?
- #The last element after import should be the path, which is the directory, not the package name. But many times, the path name is the same as the package name
import m "lib/math" m refers to the only package in the lib/math path. If the compiler finds two packages in this path, it is not allowed. Compile Error reporting
Recommended tutorial: "go language tutorial"
The above is the detailed content of What should I do if golang cannot import packages?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.
