Some problems with package import in go language

Release: 2019-12-14 16:40:07
forward
2162 people have browsed it

Some problems with package import in go language

import

Go Use packages as the basic unit to organize source code, all syntax visibility is Defined at the package level. Under the same package, there can be many different files, as long as each file belongs to the same package name.

The first line of each source code file must define which package it belongs to through the following syntax,

package xxx
Copy after login

Then it is the standard package or third-party package used to import this source code file, that is,

import (
    "a/b/c"
    "fmt"
)
Copy after login

The standard library will be searched from the GO installation directory, and the third-party library will be searched from the $GOPATH defined by the developer. When neither is found, the compiler will report an error. When using third-party packages, when the source code and .a are both installed, the compiler links to the source code.

Note: The last c in a/b/c in the above statement is the directory name, not the package name.

When calling a method in a file, use the following format:

package.Methodxxx()
Copy after login

The package of multiple files in the same folder is generally defined as the name of the folder, but there are exceptions. For example, in the chestnut above, the package of all files under the c file is defined as fux. Then when calling the method of the files under this folder, you can only use fux.Methodxxx() instead of c..Methodxxx()

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 .a), which is used for subsequent executable program linking.

vendor

Go added vendor support for package management in version 1.5. Version 1.5 needs to set GO15VENDOREXPERIMENT="1" to support this feature, and version 1.6 uses it as the default parameter configuration. The following rules for package import paths containing vendor directories are roughly as follows.

├── d
    ├── mypkg
    |     └── main.go
    └── vendor
          └── q
              ├── q.go
Copy after login

When the above directory structure is imported q in main.go, it will first be searched from the vendor directory. If it cannot be found, it will be searched from the $GOPATH directory. If it cannot be found again, the compiler An error was reported.

For more go language knowledge, please pay attention to the go language tutorial column.

The above is the detailed content of Some problems with package import in go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:studygolang.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template