当前位置:网站首页>Three efficient programming skills of go language

Three efficient programming skills of go language

2022-06-24 14:06:00 The snow is merciless

Click blue above “ Snowy and heartless ” Pay attention to me , Set a star , Read the article for the first time

Go Is a very good programming language . It is a way for you to really focus on your business , Don't worry too much about the language of the program itself , So you can write the application as soon as possible . For example, it has a relatively complete ecosystem , It can provide you with everything you need to get started .

But what? , It's not everything , There are some things we need to pay attention to . I wrote this article mainly to remind myself , Also some observations and conclusions . Of course , These are just tricks , It's not really a problem , Of course, if you notice them and use them in practice , It will benefit you very much , This is the difference between novice and veteran .

Do not use Logrus

This is really about generics . because Go Language is a strongly typed static language , So you can't be like NodeJS perhaps PHP That bypasses data types . What if we still need to use generic types ? Such as Loger, perhaps ORM, Because only common types are used , To write universal code , Otherwise everyone has to write once .

Final , We can only use reflection . and Logrus Use reflection a lot , This results in a large number of allocation counts . Although usually not a big problem ( Depends on the code ), But performance is important , Especially on a large scale 、 In highly concurrent projects . Although this sounds like a very small optimization , But it's important to avoid reflection . If you see some code that can use structures regardless of type , It uses reflection and has an impact on performance .

for example ,Logrus Don't care about type , But apparently Go Need to know ( Final ).Logrus What shall I do? ? Use reflection to detect types , This is an expense .

  log.WithFields(log.Fields{
    "animal": myWhatever,
  }).Info("A walrus appears")

So I would prefer zerolog, Of course zap Pretty good also . Both claim zero distribution , That's what we want , Because their performance impact is minimal .

Do not use encoding/json

When we need a function 、 Function , Many people recommend using the standard library . But in the standard library encoding/json Modules are an exception . In fact, it is the same as the above example ,encoding/json Using reflection , This leads to poor performance , And writing the return json Responsive API 、 Or micro services will cause losses .

For example, you can use Easyjson, It is very simple , It's also efficient. , It uses a code generator to create a structure that converts to json Required code , To minimize allocation . This is a manual build step , Is very boring . Interestingly json-iterator Also use reflection , But significantly faster , I suspect it's black magic .

Try not to goroutine Using closures in

such as , The following example code :

for i:=0;i<10;i++ {
  go func() {
     fmt.Println(i)
  }()
}

Most people might expect this to print numbers 0 To 9, It's like delegating a task to goroutine It's like that .

But the actual result : According to the system , You will get oneortwo numbers and many 10.

Why is that? ? Closures can access the parent scope , So you can use variables directly . Although updated linters May warn you “ Variable closure capture ”, But it doesn't require you to redeclare the variable .

Go The performance reputation of is largely due to the execution of runtime optimizations , It tries to “ guess ” What do you want to do and optimize some execution paths . in the meantime , it “ Capture ” Variables and pass them to where they are needed in the most efficient way in theory ( for example , After completing some non concurrent operations to release some CPU After allocation on ). The result in this case is that the loop may start goroutines,goroutines May receive... From the parent scope very late i Value . There is no guarantee which... You will see when you execute this code multiple times , It could be numbers 10, It can also be other numbers .

If you do use closures for some reason , Be sure to pass variables i, Treat closures like every function .

Summary

There must be more than these three skills in programming practice , Explore and master them in practice , It can improve our programming ability , Let's write more optimized code .

This is an original article , Reprint with reference to , Welcome to scan code to pay attention to official account. flysnow_org Or websites https://www.flysnow.org/ , The first time to see the following wonderful articles . If you feel good , Please click on the bottom right corner of the article 「 Looking at 」, Thank you for your support .

——  Highlights   ——

Do it Caddy( 13、 ... and )| Plug in development Caddyfile Parameters

Caddy actual combat ( Twelve )| Plug in development HelloWorld

  Caddy actual combat ( 11、 ... and )| Caddyfile The beauty of design

Caddy actual combat ( Ten )| Build in one minute PHP The server

Caddy actual combat ( Nine )| Set header information to realize cross domain

Caddy actual combat ( 8、 ... and )| Using buffer to improve the performance of reverse proxy

Caddy actual combat ( 7、 ... and )| Health check in reverse proxy

Caddy actual combat ( 6、 ... and )| Load balancing in reverse proxy

Caddy actual combat ( 5、 ... and )| Configure reverse proxy

Caddy actual combat ( Four )| Use API management Caddy

Caddy actual combat ( 3、 ... and )| Caddyfile Quick start

Caddy actual combat ( Two )| Caddy The most complete tutorial of command line parameters

Caddy actual combat ( One )| Hosting your website , Just one command


Image

Sweep yards attention


Share 、 give the thumbs-up 、 Watching is the greatest support

原网站

版权声明
本文为[The snow is merciless]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241259226992.html