0%

GO_Learning_Package Definition

Blog

Package is the unit of go language

In GO language, Package is the Basic unit while other language’s unit usually be file. For example,we have two hello World software in different language , Their directory tree is :

  • GO
1
2
3
root :
hello :
hello.go
  • Python
1
2
3
root :
hello :
hello.py

In go, the syntax to use hello world func is :

1
2
3
4
5
import root.hello 

func main(){
hello world()
}

In Python, the syntan to use hello world func is :

1
2
3
4
5
import root.hello.hello

func main():
hello.hello world()

What is Package

Package is a directory which including .go source file. Some features of package in Go:

Features:

  • Package name is match with directory name. If your package is called logger, your directory may be like:

    1
    $GOPATH/src/github.com/yourname/logger
    • Package name is recommend as lower case.

    • Package names, and thus the package’s directory, **should contain only letters, numbers **if you must, but absolutely no punctuation. Package name is part of type , func , var , const which export by this package.

  • All files in a package’s directory should have the same package declaration.

    • Testing file is a exception; For testing, your test files, those ending with _test.go, may declare themselves to be in the same package, but with _test appended to the package declaration.

Main package

Main package is actually commands, these carry the declaration of package main. It also conforms to the previous package specification,