Why did I switch from PHP to go?


Today, a classmate asked me why I changed from PHP to go?
I haven’t thought about it before. Here I record some advantages of go compared with PHP
This article does not mean to belittle PHP, PHP has also been spirited

The running environment is simple

Golang official provides a clear way to install the environment
PHP has many choices in the market, and there is no unified way

  1. Building PHP environment
    Anyone who has used PHP can at least build a Wamp or lamp environment, but without an official environment, who hasn’t encountered any pitfalls when installing the environment
    Of course, there are many integrated environments on the market, such asphpstudywampserveretc.

  2. Construction of golang environment
    stayOfficial website(orDomestic image)Download the installer,Keep going to the next stepthat will do

The grammar is simple and unified

PHP keyword more, the number of go three times
Go has strong syntax restriction and comes with gofmt format code
PHP has no mandatory restrictions, resulting in different formats for everyone
The way to restrict access to go variables and functions is relatively simple

  1. PHP keywords(75 in total)

    __ halt_ Compiler(), abstract, and, array(), as, break, call (as of PHP 5.4), case, catch, class, clone, const, continue, declare, default, die(), do, echo, else, else, empty(), enddeclare, endfor, endforeach, ENDIF, endswitch, endwhile, eval(), exit(), extends, final, finally (starting from PHP 5.5), for, foreach, function, global, Goto (starting with PHP 5.3), if, implements, include, include_ Once, instanceof, instead of (starting from PHP 5.4), interface, isset (), list (), namespace (starting from PHP 5.3), new, or, print, private, protected, public, require, require_ Once, return, static, switch, throw, trait (from PHP 5.4), try, unset (), use, VaR, while, XOR, yield (from PHP 5.5)
    __ CLASS__ , __ DIR__ (starting with PHP 5.3)__ FILE__ , __ FUNCTION__ , __ LINE__ , __ METHOD__ , __ NAMESPACE__ (starting with PHP 5.3)__ TRAIT__ (starting with PHP 5.4)

  2. Go keyword(25 in total)

    break, default, func, interface, select, case, defer, go, map, struct, chan, else, goto, package, switch, const, fallthrough, if, range, type, continue, for, import, return, var

  3. PHP has no strong syntax restriction

    //Curly braces can be placed after a method or on a separate line
    function test1() {
    function test2()
  4. Go has strong syntax restriction and comes with gofmt format code

    //The curly braces after a method can only go with the method name
    func main() {
  5. PHP property and method access restrictions

    //Declare with keywords
    Public $name = "can be used in itself, subclass and instance"
    Protected $name = "can be used by itself and its subclasses"
    Private $name = "only you can use it"
  6. Restrictions on access to go variables and functions

    //Use the case of the first letter
    Var name = "only in package"
    Var name = "can also be accessed outside the package"

Package management

  1. PHP package management
    usecomposerThird party package management, if you want to use, you need to go to a specialThird party websitesPublish your own package

    1. usecomposer initInitialize project
    2. useComposer install package nameIntroduction of third party packages
    3. usecomposer dump-autoloadAutomatic loading of imported third party packages
    4. Use where you need to use it, and then create an instance of new to call its method
  2. Go package management
    Go mod is officially provided for package management. Packages can be published on any website

    1. useGo mod init current package nameInitialize project
    2. useGo get package URLIntroduction package
    3. Import can be used where it is needed

operating efficiency

Here we use the quick sort method to sort and verify 1000000 random numbers
Then count the running time and the space used
Do not use the official sorting algorithm, the code is a bit long, the code on GitHub

  1. PHP code
  2. Go code
  3. Running results
    Go uses 1 / 5 of PHP’s memory
    Go is used for 1 / 10 of PHP
    Why did I switch from PHP to go?

Previous articleThere are some courses of my learning go, which can be referred to by my buddies at the beginning

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]