This is the 18th day of my participation in Gwen Challenge

These reviews

In the previous series, we covered the basic syntax of Go and some simple features, such as data types, containers, and so on. In this chapter we’ll take a look at some of the advanced features in Go, including package management, reflection, concurrency, and more.

High reusability of code is an essential quality of good code, and Go’s package management provides a powerful way to encapsulate modules and reuse code.

Go is a statically strongly typed language. During program compilation, the reflected information of variables, such as field types and type information, will be written into executable files. As the program executes, the virtual machine loads reflection information about variables in the executable and provides an interface for fetching and modifying capabilities at run time.

Go supports two forms of concurrency, the common shared memory concurrency model and the CSP (Sequential process communication) concurrency model recommended by Go. Go uses a special MPG thread implementation model that enables native support for concurrency, greatly improving program execution efficiency.

In this series of articles, I will take a closer look at the Go language and introduce advanced features such as package management, reflection, and concurrency. In this article, I will introduce the application practices of Go package dependency management.

If you don’t already know Go, I suggest you read mineLearn Go from Zero series of articles.

Dependency management

Go’s code reuse relies heavily on a package basis, and package management relies heavily on GOPATH. Go’s package management has long been criticized, but there have been significant improvements since the release of Go Modules in version 1.11. In this article we’ll take a first look at package management for Go.

Package management

As with most programming languages, there is a package definition in Go, and we can define a package through the package keyword. The following code looks like this:

package main

import "fmt"

func main()  {

	fmt.Println("Hello World")

}
Copy the code

We define the main package with the package keyword and specify in Go that the main function #main must be under the main package. Packages can be imported using the import keyword, and multiple packages can be imported together in parentheses, as shown below:

Import (" FMT "OS")"Copy the code

Func, type, variable, constant defined in the same package, code in all files under the package is freely accessible, belonging to the package public. Let’s create a new hello.go file in our main directory and add a #sayHello function to it like this:

package main

import "fmt"

func sayHello()  {
	fmt.Println("Hello World")
}
Copy the code

It belongs to the main package as well as main.go. We can use the #sayHello function directly in main.go as follows:

package main

func main()  {
	sayHello()
}
Copy the code

Since we need to compile both main.go and hello.go files, execute the following command in the main directory:

go run main.go hello.go
Copy the code

That is, the expected “Hello World” field can be printed on the command line.

If func, Type, variable, and constant are under different packages, you need to capitalize their names to indicate that they are publicly accessible. If you want to access fields in a structure outside the package, you also need to capitalize the variable name.

Add. Go add.go add.go add.go add.go add.go add.go add.go add.go add.go add.go add.go add.go

Package compute type AddOperator interface{/** * AddOperator interface */ Add() interface{}} type IntParams struct{P1 int // AddOperator P2 Int // addend} func (params *IntParams) Add() interface{} {return params.p1 + params.p2}Copy the code

Add. Go belongs to the compute package through package compute; The AddOperator interface is defined, with a single method #Add for arithmetic addition; The IntParams structure is also defined to implement AddOperator#Add, which adds two integers contained in IntParams and returns the result.

Note that interfaces, functions, structures, and fields under structures in Add.go are capitalized, indicating that they are public and accessible outside the package. Next we modify the main function to reference and use the compute struct and function defined in the main package as follows:

package main

import (
	"ch4-feature/compute"
	"fmt"
)

func main()  {

	params := &compute.IntParams{
		P1:1,
		P2:2,
	}
	fmt.Println(params.Add())

}
Copy the code

IntParams: ch4-feature/compute: ch4-feature/compute: IntParams: ch4-feature/compute: IntParams: ch4-feature/compute: IntParams: ch4-feature/compute: IntParams: ch4-feature/compute: IntParams: IntParams: ch4-feature/compute: IntParams: IntParams: ch4-feature/compute: IntParams: IntParams

go run main.go
Copy the code

Go compiles both main. Go and compute packages for us, executes main and returns the expected result 3.

summary

This article mainly introduces the package dependency management of Go language. Like other languages, the Go language defines a package through the package keyword. Func, type, variable, constant defined in the same package, code in all files under the package is freely accessible, belonging to the package public. We define the main package with the package keyword, specifying in Go that the main function #main must be under the main package.

The next article will cover more about GOPATH.

Read the latest article, pay attention to the public number: AOHO Qiusuo