Author: Ice

preface

At the front, I am an ordinary person who is modest in appearance, fanatical in heart, gentle in appearance and playful in heart. I am a programmer by profession and a golang enthusiast, partly because Golang is good and partly because other languages are bad. I was converted from Phper to Gopher. When I wrote PHP, I knew Internet software. When I wrote GO, I felt that I was finally programming.

First golang

I majored in software in college. The first programming language is C++, know the pointer, know the addition, subtraction, multiplication and division, know the programming to control software and hardware. Later, I took Java as an elective and was fooled by SSH framework for a summer vacation. Later, I entered the technical department of a community and was discouraged by HTML/CSS/JS. Having always ranked in the top 10 in high school, I really began to doubt life. Later, I joined a game platform company and began to write PHP. “Oh!” Finally, I know what is the Internet, torture my programming language c++/ Java/HTML/CSS, the original can also be this way “realising”.

I just entered the Internet, the heart is really happy. However, the repetition of things to do more, always not tired. I’m writing PHP every day, I’m adding, deleting, changing, and checking, and I’m modifying tables, adding fields, adding temporary tables. Really, it’s starting to get tired. “I heard that the latest company is using a new programming language, Golang,” and I wanted to give it a try. However, I did not dare, with doubts about myself and Golang in my heart, so I was not the first one to visit the master. When Golang became popular in the company for two months, my leader asked me to use golang. Finally, I made a name for myself. After a week of dabbling golang, I!! Found!!!!!

  1. Golang has a pointer. Isn’t it C++
  2. Golang is a compiled language, and the original client “package, package” is the same thing
  3. Golang can be used as a timer, how to use PHP instead? It’s a lot of trouble.
  4. Golang doesn’t need to write semicolons or parentheses. It looks very simple and works very well.
  5. Golang supports web services natively. You can also create a Web service without nginx
  6. . And on and on

Like golang

After trying Golang, I found I liked it.

Golang is rigorous

Redundant variables are discouraged in Golang. Gopher knows that if you don’t use the golang variable, you’ll get a compilation error! That makes me sit up and take notice! I think it takes rigor up a level, and as long as you’re Gopher, it forces you to be rigor. Golang variable is a property adhering to golang’s design concept, which is rigorous throughout Golang.

I once had a CTO tell me that every variable I define, every field I define, should have a function. Superfluous code, superfluous variables, superfluous fields, do nothing but mislead people and make the code dirty. I couldn’t agree more!

Golang performance is good

Golang’s simplicity and rigor alone won’t make it to the stage of a programmer. Unfortunately, the Golang works great! Less than C++, but better than Java, python, PHP, nodejs in terms of concurrency and resource usage for web applications. Specific data we search online everywhere. A language with simple syntax, good performance, and only a week to get started. Anyway, I was in the hole.

For a while, I loved Golang so much that I thought golang could do anything. But slowly I came to my senses. For web applications, golang is the first choice. Do data analysis, crawlers, and image manipulation in Python. But if someone says to write the Web background with Java, CMS type is fine, if it is for market users, spring Boot, Spring Cloud, I think, far, far less than using Golang-related framework, Java or do those individual applications.

Golang lightweight

Golang has another attribute, which is light. How light is it? Compare that to the Java JVM’s set of containers, and you’ll see how light they are. Golang wrote docker, a generational product that can implement a native Web service in a few lines of code. Etcd, the distributed cornerstone, was also written by Golang, and many components were contributed by Golang to the term cloud native. Golang is the best way to describe this must-ask phrase in a job interview: “microservices.”

Go-zero hit it off immediately

Then I joined a live streaming company and encountered the fluctuations of the company, which gave me time to stop and reflect. In the meantime, I like to browse Github and browse golang’s open source project with a little purpose. Gin, Iris, Go-Micro, Go-Zero, Sentinel-Golang, Gin-Vue-Admin and so on. Go-zero didn’t appeal to me at first, probably because of the blog/Github layout. But when I sank down to read the Go-Zero documentation one day and started typing the first goctl command, I was like, like! I can’t stop.

Go-zero is really comfortable to use!

When I first started working as an intern at a game platform company, I could have brought an intern or two. At that time, I had an idea, “how to get employees to type out standard, efficient code”. At that time, I thought about using go-template, but I couldn’t push it because I didn’t have enough skills. A few years later, when I met Goctl at Go-Zero, I found that there was a big guy who had realized my idea! Although I am not good at technology, I still have my dream. Go-zero is very dear to me. Make! Lier!!!!!

In one week, I finished the Github document of Go-Zero and the Language sparrow document, and also practiced several demos by myself. It really gets better and better. Can’t wait to find an actual project to put into production! Indeed, soon there was an opportunity, the company wanted to do a simple APP for launch research, allow to build a new background Web project, I! Go – zero! It’s incumbent. Let’s do it. It took another two weeks to write a back-end Web service with the following sections

  1. User plate
  2. Consulting sector
  3. Report plate
  4. Pay the plate
  5. Advertising sector

Take a snapshot of my project structure, since it is still a company project, it will not be open source for the time being, but any Go-Zeroer can understand it

A quick explanation:

  1. The API directory, which is http1.1’s Web service, connects to the front end
  2. RPC directory, RPC services, basic and API one – to – one correspondence
  3. Enum directory, enumeration directory, which is where all kinds of enumeration values are stored, and I mentioned it as a first-class citizen
  4. The model directory, the database model, are currently using goctl according to mysql generated model
  5. Utils directory, where I store my little toolkits, such as int/string/time operations, referring to the go-Zero naming method. Subdirectories usually end in x, such as intx, timex, stringx, etc
  6. The Worker directory is mainly a consumer model, such as kafka consumer, Google subscription notification consumer, etc
  7. Ws directory for websocket-related services, such as private chat

Thanks to Go-Zero, I have a simple standard for how to organize a project. The previous project structure of worker, WS and other services was chaotic, but now they refer to the directory structure of GO-Zero generation API:

  1. The main. Go entrance
  2. Etc stores the configuration,
  3. Internal Internal directory
    1. Config: configuration
    2. -Leonard: Handler?
    3. Logic: logic processing
    4. Types: Intermediate type

Go – zero idea

“Tools over conventions and documentation” is the philosophy of Go-Zero. I couldn’t agree more!

Go-zero’s biggest feature is goctl. What is goctl? It’s a device that generates code from protocol documents. For example,

  1. Goctl RPC proto a.proto-dir. Then the RPC service can be generated

  2. Mysql ddl-c-src b.scl-dir. Mysql ddl-c-src b.scl-dir. The template Model file is generated

  3. Goctl API go-api ad-api.api-dir. Goctl API go-api ad-api-dir. Can generate API files

    Combine that with Golang’s JB IDE, code reminders, code formatting, and a Go-Zero ecosystem. In a word: comfortable!!

Go – zero small tools

Aside from goctl, the other thing I admire is some gadgets from Go-Zero.

  1. Stream data processing tool: FX. I heard java8 lambda is cool and go-Zero is coming! Fx.filter ().sort ().head (), which simplifies the complex processing of arrays
  2. MapReduce reduces service latency: Mr. Finish(), Mr. Map().reduce (), say goodbye to the concurrent processing waitGroup!
  3. Etcd service discovery integration: P2C algorithm discovery service, without developers point-to-point or nginx forwarding services, install an ETCD and you are done
  4. JWT integration API: Easily have a JWT backend service
  5. Integration with Prometheus: Easily have a Golang backend service with monitoring
  6. Etc etc.

Thanks again to Go-Zero, there are many gadgets waiting for me to read the source code, time wheel/scheduler, etc. Go-zero is kind of like a mentor to me.

Go to zero

Go-zero provides a fully functional, high performance and fast developing Web framework for gopher like me. However, it doesn’t bind me as much as other frameworks do, and I can still do a lot with it. It’s not a big deal, but personalization is supported:

  1. Model layer, can access GORM instead of built-in SQLX, although there is no big guy hard to do cache breakdown and other protection functions. Or you can have both.
  2. The RPC layer does not have to be called by convention by the API layer. For example, the worker/ WS layer I captured above has no problem calling it.
  3. Middleware, generated by the API layer itself, can be pulled out to a public directory, so multiple API directories can use the same Middlerware.
  4. And so on and so on!

Go-zero is lightweight, convenient, and erudite. It contains a lot of knowledge and ideas. So far I’ve only scratched the surface, go-Zero’s K8S deployment, I haven’t started plugging in yet. Keep studying hard!

Thank you again for

  1. www.yuque.com/tal-tech/go… Go-zero language sparrow documentation
  2. Github.com/tal-tech/go… Go – zero source code