This is a creation in Article, where the information may have evolved or changed. Frankly, my team hated my way of preaching to the Go language, and they wanted me to put it in a more tactful way whenever there was a problem with our team's code base. [] (https://raw.githubusercontent.com/studygolang/gctt-images/master/web-developers/ Screen-shot-2016-01-29-at-11-57-56.png) The first programming language I've learned is PHP, which is a good language, and I can use it to build WEB applications quickly, and these applications can do what they expect. But I noticed that in order to make it usable, I would spend a lot of time looking at the cache. I also found myself relying on a number of third-party libraries to do more complex tasks, such as queues, Web Sockets, and so on. I found myself using PUSHER,RABBITMQ,BEANSTALKD and so on. It makes people feel a little bad. Similar problems arise when using Ruby,node and Python. In terms of concurrency, WebSockets, and performance, these languages can make people feel that they are incomplete. I need to rely entirely on frameworks and lots of documents, "syntactic sugars," DSLs, and, frankly, they often bring a lot of space-intensive stuff. I began to turn my gaze to the Go language. First Go is a kind of static type language, I always like this way. So I learned very quickly. Go is a somewhat lower-level language, and you will encounter pointers and memory references, and so on. I've been involved in C before, and go feels like C, but the standard library that go provides is very powerful and easy to use, so I'm shocked by the refinement of Go grammar. After a thorough study, I decided to look at how Go solves some of the problems that come with PHP writing WEB applications/APIs. How to solve Web Sockets? Go has several excellent library files. The following is an example of a Gin framework using the Gorilla websockets library ... "Gopackage mainimport (" FMT "" Github.com/gin-gonic/gin "" github.com/gorilla/ WebSocket "" Net/http ") func main () {r: = gin. Default () r.loadhtmlfiles ("index.html") R.get ("/", Func (c *gin). Context) {c.html ("Index.html", Nil)}) R.get ("/ws", func (c *gin). Context) {Wshandler (C.writer, C.request)}) R.run ("localhost:2020")}var Wsupgrader = WebSocket. upgrader{readbuffersize:1024, Writebuffersize:1024,}func Wshandler (w http. Responsewriter, R *http. Request) {conn, err: = Wsupgrader. Upgrade (W, R, nil) if err! = Nil {fmt. Println ("Failed to upgrade WS:%+v", err) return} for {T, MSG, ERR: = conn. Readmessage () if err! = nil {break} conn. Writemessage (T, msg)}} ' # # # # # # Concurrent in PHP, I had to either run a thread with some hack, such as using ' shell_exec () ' to delegate a task to a new thread, or using a separate service, such as BEANSTALKD or RabbitMQ. However, you can do this simply by adding the ' go ' keyword before a function that requires concurrent functions. For example ... ' Gogo func (test string) {dosomething (test)} ("This is a test") "or you can use channel ..." "Gopackage mainimport" FMT " Func main () {ch: = make (chan int, 2) ch <-1 ch <-2 FMT. Println (<-ch) fmt. Println (<-ch)} "I put a time-consuming task of uploading images to S3 in Goroutine to achieve near-instant uploads, no third-party services, completely local. Not so impressive for most developers, but I'm shocked by the ease of use and performance improvements that developers of PHP backgrounds have. # # # Test Unit tests can be a bit painful in PHP or Javascript. There are countless different test boxes.Racks, but none of them can be tested like the go built command to be so simple and natural. Main.go "Gopackage mainimport" FMT "func sup (name string) string {return" sup, "+ name}func Main () {FMT. PRINTLN (SUP ("Ewan")} "now our test, main_test.go ' gopackage mainimport" testing "func testsup (t *testing. T) {expected: = "sup, Ewan" outcome: = sup ("Ewan") if outcome! = expected {t.fatalf ("expected%s, got%s", expected, out Come)}} ' All I need to do is run ' go test ' and then I got ...! [] (https://raw.githubusercontent.com/studygolang/gctt-images/master/web-developers/ Screen-shot-2016-02-23-at-21-57-33.png) isn't it very simple? # # # Run speed when writing RESTful APIs with PHP, I have a lot of experience using frameworks like Symfony2 and Laravel. There is no pre-emphasis on several levels of caching; such as memory cache, operation cache, full-page caching, and so on. Code response time will be the same as a snail. Ruby is so notorious for being slow. Due to the static type of Go, the compiler nature and the native support for concurrency. Go runs very fast. Look at [frame benchmark] (https://www.techempower.com/benchmarks/), practice is the best proof. The most popular frameworks for Go are Gin and Revel, which are ranked higher in most tests than PHP or Ruby. # # # DevOps about Go I also noticed some, which shocked me so much that there was no need to deploy thousands of files, or to configure a WEB server or PHP-FPM. You don't even need to install Go on your server. Once the Go application is compiled (' Go Build '), you only have a small binary file left. You just have to run a separate file. Go also has a very solid built-in HTTP server ... "' Gopackage mainimport (" io "" net/http ") func Hello (w http. Responsewriter, R *http. Request) {io. WriteString (W, "Hello world!")} Func main () {http. Handlefunc ("/", hello) http. Listenandserve (": 8000", Nil)} ' # # # Grammar Go syntax is not as beautiful as Ruby, or as simple as JavaScript. But it's simple, it makes people feel lower, but Go makes people feel strong and expressive. We've all seen the traditional PHP code and feel unwell. In contrast, Go is very easy to read. Another incredible benefit of GO is that you have a good "best practice" when you write the go code. Of course, PHP has a PSR standard, etc., but they are fairly new and developers are slow to adopt them. And the language designer of Go was clear from the beginning. [Formatting Tools] (Https://blog.golang.org/go-fmt-your-code) is also built into the language ecosystem and can be run using ' Go fmt '. So my point of view is why I'm totally addicted to go and now I can't go back to PHP.
via:https://ewanvalentine.io/why-go-solves-so-many-problems-for-web-developers/
Author: Ewan Valentine Translator: Dingo1991 proofreading: Rxcai
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
1672 reads ∙1 likes