当前位置:网站首页>Go language - use of packages

Go language - use of packages

2022-06-23 05:27:00 Crying while learning

GO In language , Packages are actually directories one by one

  1. be-all go Clouds cannot be placed under the same bag , Subcontracting management is usually required ;
  2. Under the same package , be-all go file package Statement to be consistent ;
  3. main Package only the main function can be used ;
  4. Packages can be nested , But you can't loop and nest .

Usually the directory structure can be as follows :

gopath/project_name/package

go working space / project / package

Import package

// Import a single package 
import "package_name"

// Import multiple packages 
import (
    "package_name1"
    "package_name2"
)

Absolute path : from goroot perhaps gopath In the directory of src Look for the package in the directory

Relative paths : Relative to current go The location of the file

Point operation

import (
    . "fmt"
)

Add... Before the imported package . You can call functions under the package without writing the package name .

As originally fmt.Println, Ahead fmt. It can be omitted .

Starting point name

Alias operation , You can use aliases instead of package names .

import (
    p1 "package1"
    p2 "package2"
)

_ operation

If you import a package , Not to execute the functions in the package , Just to execute the init() function , You can use _ operation .

import (
    _ "package1"
)

原网站

版权声明
本文为[Crying while learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230335403080.html