Preface
As a PHP programmer, I feel honored. However, in the changing times, one must have sufficient knowledge to survive.
Let’s start with Go linguistics.
I hope you can have a basic understanding of Go after reading this article. This series of articles describes the way I learned Go language. The comparison between PHP code and Go code is used to distinguish and understand.
This is the last chapter of the transition notebook. I can’t write it down any more. I need to read it in detail. This chapter compares PHP and Golang in actual combat.
Here, Laravel and Beego (MVC Framework Developed Based on Go) are used to demonstrate
Installation
Laravel
//Direct installation via composer is simple and easy to use
composer global require "laravel/installer"
//Create a project
laravel new blog
Beego
// go has its own package management
go get github.com/astaxie/beego
//Creating a project is also very simple
bee api blog
directory structure
Laravel
// laravel's structure will not be described here.
| - app
| - bootstrap
| - config
| - database
| - public
| - resources
| - routes
| - storage
| - tests
| - vendor
Beego
//Obviously, beego is not over-designed like laravel (although over-design does not refer to directories,
//But look at the catalog and you will know that beego really doesn't have too many things)
blog
├── conf
│ └── app.conf
├── controllers
│ └── object.go
│ └── user.go
├── docs
│ └── doc.go
├── main.go
├── models
│ └── object.go
│ └── user.go
├── routers
│ └── router.go
└── tests
└── default_test.go
Routing
Laravel
Route::get('/user', 'UserController@index');
Beego
//Similar to laravel
//For the sake of unification, there are only the following methods for routing directly to bind controllers.
// beego also provides annotation methods. See https://beego.me/docs/mvc/controller/router.md for details
beego.Router("/user",&UserController{},"get:index")
Model
Laravel
<? php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Data table associated with the model.
*
* @var string
*/
protected $table = 'user';
}
Beego
// Beego uses the structure name as the table name, and all fields of orm operation must be declared in advance
package models
import (
"github.com/astaxie/beego/orm"
)
type User struct {
Id int `json:"id"`
Tel string `json:"tel"`
Password string `json:"password"`
Status string `json:"status"`
}
func init() {
orm.RegisterModel(new(User))
}
Controller
Laravel
<? php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Displays the profile for a given user
*
* @param int $id
* @return Response
*/
public function index($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
Beego
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
//This is equivalent to inheriting the parent beegoController
type MemberController struct {
beego.Controller
}
func (c *CityController) Index() {
var results []orm.Params
orm.NewOrm().QueryTable("member").
Values(&results)
c.Data["json"] = results
c.ServeJSON()
}
Summary
I have been studying Go for more than a month. Writing such articles has also met with a lot of doubts. As a notebook, it is published only to give friends who want to learn other languages an understanding of the new language, not to compare its differences. But look for grammatical similarities.
Language is only a tool. I hope that PHP engineers will not only use PHP. This year, they have seen many articles about the future of PHP programmers, but have never proposed to learn other languages.
Don’t circle yourself in a field, don’t be a frog in the bottom of a well.
Thank you
Thank you for seeing here. I hope this article can help you. Thank you