web123456

Go language customization packages

In Go, package is an important way to organize code. It allows you to encapsulate a set of related functions, variables, etc. in a single unit, making it easy to reuse and maintain the code. In addition to usingstandard libraryIn addition to packages in Go, we can also create and use custom packages on our own. In this technical blog, we will introduce the knowledge of custom packages in Go language, including the structure of packages, exported and non-exported functions, initialization and use of packages, etc., as well as related sample code.

Structure of the package

In the Go language, a package usually consists of multiple source code files. Each source file starts with a.gois the extension and the filename is the same as the package name. For example, a file namedmypackagepackage may consist of the following files:

mypackage/
├── 
├── 
└── 
  • 1
  • 2
  • 3
  • 4

Among them.is the package's entry file, which defines the name of the package and the exported functions, variables, etc.cap (a poem)contains the other functions, variables, etc. in the package, respectively.

Export and non-derived function

In the Go language, only functions, variables, etc. that begin with an uppercase letter can be used by other packages, called exported functions. Functions, variables, etc. that start with lowercase letters can only be used by this package and are called non-exported functions. Example:

package mypackage

// Export Functions
func ExportedFunc() {
    // ...
}

// Non-exported functions
func nonExportedFunc() {
    // ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

In other packages, we can pass the()to call exported functions, but not non-exported functions directly.

packetinitialization

In the Go language, package initialization is automatic. When we import a package, Go runtime automatically calls the package'sinit()function.init()Functions have no arguments and no return value, and they are usually used to do package initialization work, such as initializing variables, opening files, and so on. A package can have more than oneinit()functions, which are executed in the order in which they are defined. Example:

package mypackage

import "fmt"

// Initialize function 1
func init() {
    fmt.Println("mypackage init 1")
}

// Initialize function 2
func init() {
    fmt.Println("mypackage init 2")
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

When we import themypackagepackage, the Go runtime will execute the twoinit()function with the following output:

mypackage init 1
mypackage init 2
  • 1
  • 2

Using customized packages

In the Go language, the following steps are usually required to use custom packages:

1. Creating customized packages

First, we need to create a custom package and encapsulate the relevant functions, variables, etc. in it. For example:

package mypackage

// Export Functions
func ExportedFunc() {
    // ...
}

// Non-exported functions
func nonExportedFunc() {
    // ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2. Compiling customized packages

Then, we need to use thego buildcommand compiles the custom package and generates the corresponding binary file. Example:

$ go build mypackage
  • 1

3. Use of customized packages

Finally, we can use custom packages in other Go programs. This usually involves using theimportstatement to import a custom package and use the package name to call functions, variables, etc. in it. Example:

package main

import "mypackage"

func main() {
    mypackage.ExportedFunc()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

In this sample code, we imported themypackagepackage and calls theExportedFunc()function.

sample code (computing)

Below is a complete sample code demonstrating how to create and use a custom package:

// mypackage/

package mypackage

import "fmt"

// Export Functions
func ExportedFunc() {
    fmt.Println("ExportedFunc called")
}

// Non-exported functions
func nonExportedFunc() {
    fmt.Println("nonExportedFunc called")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
// 

package main

import "mypackage"

func main() {
    mypackage.ExportedFunc()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

In this sample code, we first create a file namedmypackagecustomization package, which contains an exported functionExportedFunc()and a non-derivative functionnonExportedFunc(). Then, we have a new section in theThe file imports themypackagepackage and calls theExportedFunc()function. When we run theThe following message is output when the program is run:

ExportedFunc called
  • 1

This means that we successfully created the custom package and used it in other programs.