In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what is the difference between the Golang standard library and the external library. I hope you will get something after reading this article. Let's discuss it together.
Foreword:
I've been using Go in production for some time, and I like its speed and reliability because of its small build scale, the concurrency performance provided by goroutines, and the ability to run builds directly on the machine.
Because the speed of the standard package is very fast, you can build production-ready microservices without using any third-party libraries or frameworks. This is not to say that there are no frameworks in Go that provide more flexibility or speed, but they are not so popular.
Officials usually tell you to stick to the standard library. Ironically, the top Google search results of the golang framework are generally about why standard libraries should not be used.
I did some research and benchmark tests on the alternatives to the standard library to understand their performance. I divide them into what I think are key micro-service components.
All benchmark tests are run on the configurations listed below. Although the normal benchmark time is only 1 second, I ran all the tests for 10 seconds to ensure that each test cycle is consistent.
Processor-2.7 GHz Intel Core i7
RAM-16GB-16GB 2133 MHz LPDDR3
Bench Time 10s instead of standard 1s
Go test-bench=. -benchtime=10s
1. Routing
The standard http Go server comes with a nice router that can read query parameters but not named parameters, such as
/ students/:studentID/grades/:gradeID
Any REST service with the above nested resources must use an external routing library to resolve them. Gin, Echo, Beego, Gorilla Mux, and Goji are just a few of the well-known frameworks (based on Github concerns).
Some of them are mature middleware frameworks with routing capabilities, while others are just routing libraries.
These libraries were tested against a single named parameter, and the results are as follows: Gin has the fastest router, with Echo in second place.
2. JSON serialization and deserialization
Once the API request passes through the router and is passed to the controller or handler, the next step is to decode the request JSON or Encode when the response is returned.
Go has a very good encoding package that supports a variety of formats, such as json, XML, and csv, but a quick glance at alternatives will show you a lot of libraries. I benchmark Jsoniter and EasyJson for the standard encoding/json package, and the results are as follows.
Here are the results of the coding, which show that the performance difference is not significant.
However, for decoding JSON,jsoniter, the execution speed is 5 times faster than that of standard encoding packets.
Now, if your request has been decoded, the next step may be to apply your business logic and perform some database operations.
3. Whether to use ORM framework or not
Most popular languages rely on frameworks to build micro-services that interact with databases. In the Java world, Hibernate, Active Record for Rails and Django ORM are very popular. ORM (object relational mapper) sometimes helps to better handle transactions, relationships between tables, and helps avoid writing complex SQL for simple joins.
But Go once again has a very good database standard library, which makes it very easy and fast to connect to relational databases. However, one pain point is query. When you query certain rows, you must manually map each field in the row and then assign them to struct. Exe. This works, but it quickly becomes messy and allows you to write a lot of code. Sqlx is a library that allows you to scan entire rows into your structure variables.
Although sqlx reduces the number of typical lines written to build CRUD, it ends up writing repetitive code multiple times. Using ORM can help reduce it and focus on your business logic.
Database, database + sqlx, gorm, go-pg benchmark the query. Here are the results. Surprisingly, go-pgORM executes faster than standard packages or even sqlx. Although GORM is very famous in the ecosystem, it is relatively slow.
At each step of the API call, there are better frameworks or external libraries that will make your response faster and provide some flexibility.
After reading this article, I believe you have a certain understanding of "what is the difference between Golang standard library and external library". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.