Use of golang import and package

  • 2020-06-19 10:32:30
  • OfStack

golang USES the package package to manage the definition module, which can be imported using the import keyword.

If you import a package that comes with go, the installation directory $GOROOT/src is loaded in the package path, such as the fmt package If it is our go get installation or custom package, it will be loaded under $GOPATH/src

The definition of package

The location of package is $GOPATH/src as the root directory, and the package name should be the same as the last level 1 directory name.

Such as our custom baz bag, bag module's location is $GOPATH src/foo/bar/baz baz package source code are stored in the directory, foo bar/baz import is as a package path are loaded.

We need to formally define package as baz for the source code of baz package, so we have defined a package that can be loaded by import.

hello module


//$GOPATH/src/foo/bar/baz/hello.go
package baz

import (
  "fmt"
)

//  Module initialization function  import  When the package is called 
func init() {
  fmt.Println("hello module init function")
}

func Hello() {
  return "hello"
}

world module


//$GOPATH/src/foo/bar/baz/world.go
package baz

import (
  "fmt"
)

//  Module initialization function  import  When the package is called 
func init() {
  fmt.Println("world module init function")
}

func World() string {
  return "world"
}

package main

import (
  "fmt"
  "foo/bar/baz" // Introduce our custom package 
)

func main() {
  fmt.Println(baz.Hello(), baz.World())
}

The use of import

Normal operations


import (
  "fmt"
  "log"
  "foo/bar/baz"
)

A normal import is a load mechanism that imports the package to be used and then calls the methods in the package using packageName.MethodName. Note that package methods need to be capitalized if they are called in other packages, for example: fmt.Println () fmt.Printf ().

The alias operation


package main

import (
  "fmt"
  myBaz "foo/bar/baz"
)

func main() {
  fmt.Println(myBaz.Hello(), myBaz.World())
}

If there is a conflict between two package names, or if the package name is too long and needs to be abbreviated, we can use alias import to resolve it.

Point of operation


package main

import (
  "fmt"
  . "foo/bar/baz"
)

func main() {
  fmt.Println(Hello(), World()) //  Just use the in-package method   There is no need to explicitly use the package name 
}

.Imports enable methods in a package to be registered in the context of the current package, and simply call the method name without the need for a package prefix.

Underline operation


package main

import (
  "fmt"
  _ "foo/bar/baz"
)

func main() {
  fmt.Println(baz.Hello(), baz.World()) //  error  _  No packages have been imported   Just introduce and execute package modules  init  methods 
}

_ is a package reference operation that only executes the init method in each module under the package. It does not actually import the package, so you cannot call other methods in the package.

It is believed that for many phper es87EN4-compliant Namespace will use the path-dependent namespace as part of the class name as well, while golang will only use the module directory folder name as the package name. The previous path is only used for import and has nothing to do with the package name.


Related articles: