Go-spring is a GoLang application framework that mimics Java’s Spring family bucket implementation. It still follows the principle of “habit over configuration” and provides dependency injection, automatic configuration, out-of-the-box, rich third-party class library integration and other functions, which can let programmers write a lot less boilerplate code.
1. Introduction
At the end of last year, my team changed its technology stack from PHP +Java to Golang +Java due to business adjustments. Golang was a bit of a shock when I first encountered it, first because of its unusual syntax and then because it didn’t have a mature and usable development framework. If the syntax problem lasts for a long time and the code is written more, it will gradually get used to it. However, without a handy development framework, the development efficiency and code quality will be greatly affected. As a senior Java + Spring family barrel developer, I hope to change this situation. After a period of use and exploration, I found that it is possible to come up with a solution like Spring Family bucket (Spring Framework +Spring Boot +Spring Cloud)!
The Spring bucket’s place in the Java world goes without saying. It not only proves to Java developers that annotation based development, AOP based development, and interface oriented development can bring tremendous flexibility to programs, More importantly, it brings good development practices such as dependency injection, declarative transactions, unified exception handling, automated module loading, simpler Maven management, simpler unit testing, and more.
But GoLang is different from Java, so why do I believe I can do it? To answer this question is to answer another question: what language features of Java support those core capabilities of the Spring bucket, and what language features of GoLang are similar?
Java’s bytecode, reflection, annotation, package scanning and other mechanisms support the Spring bucket to achieve AOP development, dependency injection, declarative transactions, automatic module loading and other core features. GoLang cannot implement AOP because it does not have bytecode. GoLang, however, has Tags, Reflection, _ Imports, and init(), so while it’s not necessarily Java elegant, it can still implement dependency injection and automatic module loading, which are the core features of Spring’s bucket family. And while GoLang doesn’t implement AOP, Middleware does the same thing.
After some exploration and practice, go-Spring was born! In my opinion, Go-Spring was born with a rebellious and innovative spirit, just like GoLang itself. GoLang emerged as a different syntax from the mainstream programming language, while Go-Spring emerged as an absolute Java feature in doubt, such as interface oriented and dependency injection.
2. The characteristics of
Go-spring is a GoLang application framework that mimics Java’s Spring family bucket implementation. It still follows the principle of “habit over configuration” and provides dependency injection, automatic configuration, out-of-the-box, rich third-party class library integration and other functions, which can let programmers write a lot less boilerplate code. To sum up, Go-Spring has at least the following five features:
Extensible launcher framework helps you organize code elegantly
The following image shows a RTMP server’s startup function, which is only partially captured here. You can see that the startup function code is too long and needs to be carefully organized to make the code readable.
The go-Spring launcher framework encapsulates these startup processes in separate files, making functionality more cohesive and code cleaner. The following figure shows a packaged startup file.
After using go-Spring’s launcher framework, the startup process becomes a very simple one line of code!
Face interface + dependency injection, flexible replacement solutions
Go-spring provides a unified API for Redis services, but the underlying implementation has multiple alternatives. Users only need to pay attention to the API interface when writing business code using Redis services, and do not need to care about the underlying scheme.
Of course the user will eventually choose an underlying implementation of the Redis service, which is only a line of code away!
If you want to switch to another redis underlying implementation, it’s just a line of code.
Automatic binding configuration, simplify the use of configuration files
In code that uses Go-Spring, just set the name of the configuration item to bind to the variable and add the configuration item to the configuration file. Go-spring will automatically bind the variable to the configuration item for you.
Go-spring also supports binding different configuration files based on the runtime environment. For example, Go-Spring uses the application-test.properties configuration file when detecting an offline environment. The application-online.properties configuration file is used when an online environment is detected.
Effectively help to do a good job of project dependent management
Go-spring provides an abstract interface for each module, and users do not need to care about how the interface is implemented internally, which makes it easy to solve the dependency upgrade problem. Go-spring guarantees that all released versions of project dependencies are correct, and go-Spring updates dependencies with each release, so users only need to pay attention to go-Spring version changes to take advantage of automatic upgrades for other dependencies!
Make complex unit testing easier
GoLang’s unit testing, especially for HTTP, sucks! A project launched using Go-Spring can use a real project runtime environment for unit testing, rather than a fake HTTP environment.
3. The component
Go-spring consists of four core projects:
- Go-spring implements core functions such as IoC container and dependency injection.
- Go-spring-boot provides automatic configuration and application startup framework;
- Go-spring-cloud builds a microservice framework for everyone based on the open source world;
- Go-spring-didi focuses on didi’s internal technology to achieve a microservice framework with Didi’s characteristics.
Example 4.
I’ll show you how to use Go-Spring with a simple HTTP service.
1. Create the main.go file, create the startup program, and specify the directory where the configuration file resides.
2. Import the Echo HTTP service into the program.
3. Create an example.go file, implement a sample service, and register the HTTP interface route in InitControllr().
4. Register the object of a sample service with go-Spring’s IoC container so that Go-Spring automatically loads the route for the HTTP interface registered by the user.
5. Introduce the package containing the sample service in the main.go file so that the Go-Spring framework can load the module containing the sample service at startup.
Using curl http://localhost:8080/ to test the HTTP service, you can see the following results:
{"code": 900001,"msg":"biz error"}Copy the code
OK, are you starting to feel the power of Go-Spring? Let’s look again at how unit tests would be written for a project that uses the Go-Spring framework.
First, we can write a TestMain function to start the real HTTP server.
The actual code shown above could be even simpler, reduced to one line.
We can then write a test function to send a real HTTP request without fake or mock.
Run this unit test and you will find that you have a fully functional breakpoint debugging test environment that is completely mock-free and fake-free.
5. To summarize
In addition to the ability to create HTTP services and unit tests shown above, Go-Spring already supports mysql services, Redis services, Kafka services, DDMQ services, service registration and service discovery services, and a variety of RPC services. And more new components and features are coming in through the didi source, so go-Spring will become more and more perfect and easier to use in the future!
Another Go Style! I personally think go-Spring represents a new way of programming, and even a new way of productivity, and I hope that you can use go-Spring to free your mind, be more productive, have more fun and freedom, and spend more time with your friends and family!
This article is first sent from Pratt & Whitney Travel Product Technology (ID: PZCXtech)