Hello! I want to share my first Go programming experience.
I started my way into programming as a frontend developer. Then I switched to the backend in Python and, a little for myself, to C # Asp.Net MVC. But just recently, a couple of months ago, I met this beautiful Go language.
Why go?
1 - compiled language.
2 is a very unusual language.
')
Structures, interfaces ... I had to break my brain, relearn. And the first problems, it's over, it is just to relearn. Where are the classes? Where is OOP? Where are your favorite patterns?
But having broken my head a little, the Go language opened up to me and I fell in love with it. And, of course, the first thing I decided to write my first "bicycle", namely the web framework called MGGO.
What I wanted and what I wanted
Of course, I immediately wanted to make the MVC framework like Asp.Net. But it turned out to be not so easy, because the Go language is peculiar. But what happened:
Controllers (controller)
The controller is a structure in which methods can be both external (api) and only internal, as well as, which are responsible for the view.
At once on an example we will consider the news controller.
import mggo type News struct{ ID int Title string Text string } func NewNews() *NewNews{ return &NewNews{} } func init() {
After that, news is available via the link / news /
Where news is the name of the controller, and the main page is the index for which the IndexView method is responsible.
Now, about api. The News controller has a Read method. By default it is only service. In order to allow api request to this method, you need to add permissions to the method of the controller βinitβ.
func init(){ ... mggo.AppendRight("News.Read", mggo.RRightGuest) }
After this, the Read method is available for api calling any user.
fetch("/api/", { "method": "POST", "headers": { 'Accept': 'application/json', 'Content-Type': 'application/json' }, "body": JSON.stringify({ "method": "News.Read", "params": {} }) }).then(res => res.Json()).then(console.log)
Result
{result: {ID:1, Title:"First News", Text:"Text first News"}}
A little about the parameters. All parameters inside the params object will be passed as structure fields.
For example, let's slightly change the Read function:
func(n *News) Read(ctx *mggo.BaseContext) News{ if n.ID ==1 { return News{1, "First News", "Text first News"} } else { return News{} } }
And the api call must be with the parameter ID: 1 - params {ID: 1})
What else?
PostgreSQL. The
go-pg library is
included in the framework. Let's take an example right away. Our news controller has static data. Need to change this. For this, the first step is to create a News table. Add a couple of lines to the init function.
func init(){ ... mggo.InitCallback(func(){ mggo.CreateTable([]interface{}{(*News)(nil)}) }) }
Now, after initialization, we get a table of news, with the fields id, title and text.
Change the Read method:
func(n *News) Read(ctx *mggo.BaseContext) News{ mggo.SQL().Select(n) return *n }
If we call the News.Read method with the ID: 1 parameter, then as a result we will receive data from the news table with the key 1.
Go-pg features can be found in the
documentation .
But that is not all. All describe here will not work. I hope I will write and lay out the detailed documentation soon. In short, what else can Mggo.
β Web sockets
β Server and client events
β Caching method results
β JsonRPC call support
β Liba for quick creation of the controller and view
That's all for now. I hope someone article will be useful. Of course, there are a lot of other ready-made and cool frameworks. I just wanted to try to do something for my own personal experience.
Link to the project
Link to example and quick start