There are things Go excels at and there are things PHP excels at. They don’t always overlap. For example, you can write a simple web app in 100 lines of php code, and a process manager in 100 lines of Go code. If you flip the languages around, the number of lines explodes.
I started writing PHP back in the 3 and 4 days... I have been writing go for over a decade.
If I had to add a few dynamic elements to a static HTML site, php is still king.
If we're building an API, go will win hands down. Not only in LOC but performance, and reporting.
When you get into whole pages (think blogs or shopping or... ) a lot of that is going to depend on what framework you're grabbing on the PHP side. There arent a lot of "batteries included" solutions on the golang side... You might end up writing fewer lines of code in the first week but by the end of the month were going to be at feature parity and your quickly going to fall behind.
The moment I need a SPA, on page interactive GUI you're going to end up with a JS framework and an API back end... here again, go will shine.
> If we're building an API, go will win hands down.
I'm not so sure of that. Writing business logic in PHP is usually more concise than in Go, and more flexible. Also, I can think of a number of json schemas that are impossible to replicate in Go's type system but work just fine in PHP.
>> Also, I can think of a number of json schemas that are impossible to replicate in Go's type system but work just fine in PHP.
Not at all. There are some that would be painful to write "by hand". An expansive and nested set of null fields would suck if I had to spell it all out...
https://sqlc.dev << changes everything. If you add in the YAML (and I hate yaml) you can get your JSON to DB mapping in there, as well as your validations (all output as struct tags).
Everything else that you're going to want (transforming inputs to/from json, logging, auth) is some pretty simple middleware.
You can't fully replicate JSON schemas in either language without writing logic.
In terms of just static definitions, Go has typed container types while PHP has not. That's a pretty common case for JSON. PHP is more convenient at another common case which is heterogeneous maps. Neither of those languages make me particularly happy in that regard.
It also depends on what you're doing with the JSON. If you want/need to walk through it in a single pass fashion, then Go is definitely more fitting.
> PHP is more convenient at another common case which is heterogeneous maps.
Or arbitrary, user-defined keys...
> If you want/need to walk through it in a single pass fashion, then Go is definitely more fitting.
array_map, array_reduce, array_column is pretty good at walking through arrays very quickly, assuming you parsed the json into arrays instead of objects.
If you use the std lib you can write a simple web app in Go as well in few lines of code?
The only difference would be a little bit of boilerplate for setting up application state (a few lines) and the necessary type definitions, which you can avoid with PHP if you just deal in "arrays" instead of classes. But I don't think the LOC would explode for Go.
My big counterpoint is this however:
I would wager for a beginner of either languages, it's much more straight forward to get a server running with Go than with PHP/Apache. There's no configuration, you don't need to install multiple things, you don't need to learn about a thousand footguns, error handling is more streamlined and obvious and you wouldn't have version issues and get showered in deprecation warnings down the line. And even though the PHP docs are quite extensive and useful, they are not on the same level as authoritative Go docs and guides. PHP has three advantages over Go in terms of convenience and ease of use: statelessness, little bit easier to deploy (once set up) and very cheap, easy to use hosting products available.
> If you use the std lib you can write a simple web app in Go as well in few lines of code?
PHP is made for web development though, while Go is made for different use cases. Comparing the standard libraries makes this very clear.
Things PHP comes with that Go doesn't come with (just as some examples about standard library differences): PDO (DB interface), session management, built-in mail sending, localization and internationalization. That's just from the top of my head.
Go has it's places though, and it works well for web development too. But even I who like Go in general more than PHP, can acknowledge that PHP (by default) is better setup for web development out of the box.
That said, I would much prefer Go than PHP too, but hard to argue against PHP for web development without resolving to emotions and feelings.
Apart from localization all of the things you mentioned come with Go as well. There's net/smtp, database/sql. You can implement in-memory sessions in Go with a few lines or use the well established gorilla sessions or similar.
The only thing you _need_ to install via dependencies is a DB driver.
On top of it you are getting a HTML template system with very good security defaults. You are generally better guided and guarded when dealing with HTTP user input. Routing and handling is more explicit and doesn't require you to deal with the Apache configuration syntax etc. There are no arcane configuration footguns like max_input_vars that will silently break your application and so on.
Errors handling and logging are again, more straight forward, universal and explicit.
If you need interactivity, SEE and Websockets are not only easier to use but also a more natural fit for Go.
I think for a language beginner those things are very, very beneficial. Us more experienced programmers sometimes forget how many battle scars we had to earn with languages like PHP.