Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the composition of the Go package?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the composition of the Go package?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Packages and their dependencies

When developing micro services, it is useful to split services by component. Each component should be independent and, in theory, can be extracted to an external service if needed. How to understand and realize it?

Suppose we have a service that handles everything related to an order, such as sending email confirmation, saving information to a database, connecting to a payment provider, and so on. Each package should have a name that clearly describes its purpose and complies with naming standards.

This is just an example of our project with three packages: confemails,payproviders and warehouse. The name of the package should be as short as possible and clear at a glance.

Each package has its own Setup () function. This function receives only the most basic parameters that make the package run. For example, if the package provides HTTP services, the Setup () function only needs to accept a HTTP route similar to mux route. When the package needs to access the database, the Setup () function accepts only the sql.DB parameter. Of course, this package may also need to rely on another package.

The composition of the package

Knowing the external dependencies of the module, the next step is to focus on how to organize the code within the module (including the handling of related dependencies). In the beginning, the package contains the following files: setup.go-which contains the Setup () function, service.go-it is a logical file, repository.go-it is a file that reads / saves data to the data.

The Setup () function is responsible for each building block of the module, that is, services, repositories, registered event handlers, HTTP handlers, and so on. This is an example of actual production code using this approach.

Func Setup (router * mux.Router, httpClient httpGetter, auth jwtmiddleware.Authorization, logger logger) {

H: = httpHandler {

Logger: logger

RequestClaims: jwtutil.NewHTTPRequestClaims (client)

Service: service {client: httpClient}

}

Auth.CreateRoute ("/ v1/lastAnswerTime", h.proxyRequest, http.MethodGet)

}

In the above code, it builds the JWT middleware, a service that handles all the business logic (and the location of the logs) and registers the HTTP handler. Because of this, modules are very independent and (theoretically) can be transferred to separate microservices without much work. Finally, all packages are configured in the main function.

Sometimes we need some handlers or database drivers. For example, some information can be stored in a database and then sent to different parts of the platform through events. It is inconvenient to use a method like saveToDb () to save data only in the same library. All similar elements should be separated by the following functions: repository_order.go or service_user.go. If there are more than 3 types of objects, move them to a separate subfolder.

test

When it comes to testing, I adhere to some principles. First, use the interface in the Setup () function. These interfaces should be as small as possible. In the above example, there is a httpGetter interface. There is only the Get () function in the interface.

Type httpGetter interface {

Get (url string) (resp * http.Response, err error)

}

Thankfully, I only need to simulate one method. The definition of an interface needs to be as close to its purpose as possible.

Second, try to write fewer test cases and cover more code at the same time. For the decision / operation of each main function, one successful test case and one failed test case should be sufficient to cover about 80% of the code. Sometimes, there are key parts of the program that can be covered by separate test cases.

Finally, write the test in a separate package with the suffix _ test and put it into the module. It's useful to put everything in one place.

When you want to test the entire application, prepare each dependency in the setup () function next to the main function. It will provide the same settings for both the production and test environments, avoiding some bug for you. Tests should reuse the setup () function and simulate only those dependencies that are not easy to simulate (such as external api).

This is the end of the content of "what is the composition of the Go package?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report