📜 ⬆️ ⬇️

As I have been rewriting my cryptocurrency with PHP for Go for 8 months. Part 2



The 2nd part of the story, which began with a text message from Kati "Do not call and do not write me more !!!!"

The first part is here . For those who are too lazy to read, I will give a summary:

4.5 years ago I had the imprudence to start writing my cryptocurrency in a completely inappropriate language for this business - in PHP. As a result, of course, I wrote (I am stubborn), but it turned out to be a crutch on a crutch and that it worked at all was just some kind of magic.
I just want to warn you, I’m a self-taught programmer and I write code, to say the least, imperfectly.
')
It all started with the fact that I broke up with a girl named Katya and on the same day (April 4, 2015) decided to study Go and rewrite my cryptocurrency. For those who are interested in what's up with Katya, I made spoilers, but who are not interested, just do not pay attention to Katya

For those who did not read the 1st part about Katya
I'll tell you first how it all began. I met her on the Internet on a dating site in early 2015. Fell in love almost immediately. It hurt outwardly she was in my taste. We walked in the park, joked, laughed, made fun, it became cold, we did not want to part. He called me to watch cartoons. We got into a taxi, we arrived. Turned on "Three heroes: Knight's move", a very funny cartoon, lay on the bed, laughing to tears.

Late evening, I am sure that Katya will stay overnight. But for some reason she began to repeat that she needed to go home. I said I wanted to feed her breakfast in bed. Agree that tomorrow morning she will arrive for breakfast. Called a taxi, she left. I fell asleep happy because I found a beautiful girl with whom it is fun and interesting. The next day, a text message came from her “Hello. What are you doing?". I remember my feelings, because I wrote a girl who I really like, it was kayfovo. And soon we were to meet with her again ...

That day we never met. And the next one too. I do not know what was going on in her head, but she constantly endured dating. Not even congratulated on February 23.
I decided to shake it up somehow. Wrote Kate sms-ku: "I will soon, of course, grab"
She replied: "in a sense?"
I: “Oops, not there”
She: "that means how"
She got jealous. A day later offered to meet, she agreed.

After analyzing her behavior and, a little googling, I came across the book “New rules. Secrets of a successful relationship for modern girls "
During the evening I read and understood that for some reason she fell in love with herself using the advice from this book.

A total of 8 months: the application runs on Win ( 64/32 ), OSX (64/32), Linux (64/32), FreeBSD (64/32), Android , IOS (it will be cool if someone throws on the App Store ).
General code ~ 73k lines, code for different OS somewhere a few hundred lines.
40k - processing / generation of blocks / tr-y, 17.5k - controllers for the interface, 15.5k - templates
PostgreSQL, SQLite, MySQL are supported.

Those who will test my creation, I warn you - there may be bugs, and if you have time, write about them, please, at darwin@dcoin.club or in a personal on Habré. Suggestions and advice are also welcome.

In the first part, I talked about how a web server operates in dcoin.

In this article I will describe how I use the html / template

Templates


To generate HTML pages in Go there is a very convenient package - html / template. The easiest way to understand how it works is to experiment with this example . As a rule, the templates are stored in separate html files, but in the examples on play.golang.org, for clarity, the templates are inserted into the go-code.

Most of the information on the templates I received from gohugo.io/templates/go-templates

My templates are here . Each template begins with the definition of its name {{define "templateName"}}, and ends with {{end}}

Controllers that use templates get html code through the function makeTemplate . In it, I pass the name of the html file, the name of the template (which is specified in {{define}}), parameters.

TemplateStr, err := makeTemplate("template_name", "templateName", &tplPage{ Alert: c.Alert, Lang: c.Lang, TxType: txType, TxTypeId: txTypeId}) 

The set of bytes received in TemplateStr is transferred to content.go and as a result, the browser gets the generated html-page

In the makeTemplate for all templates, I include additional templates, such as signatures.html, alert_success.html. And also, add custom functions (FuncMap), which are used to process data in templates.

About Katya
On the second date, we just walked, chatted. All was good. About my sms-ku "I will soon, of course grab" she, to my happiness, did not say anything.
I was not bothered by the question of the book on which she seemed to me to fall in love with me. I did not dare to ask directly. I wondered what he was reading. She named some authors. When asked about books on psychology, she replied that she reads for herself.
It was cold. I walked to the house. We agreed the other day to go to the rink.

Inclusions in templates


To include another template in the template you need to use the {{{template "signatures" construct }}. The dot at the end says that you need to process all the parameters in the signatures template; without a dot, the parameters will not be processed. I add signatures to almost all templates, here’s an example

Options


Comparison I will cite several examples at once to show how simple it is:
 {{if not .UserID}} <span></span> {{end}} {{if .UserID}} <span> </span> {{end}} {{if eq .UserID 0}} <span>UserID == 0</span> {{end}} {{if ne .UserID 0}} <span>UserID != 0</span> {{end}} {{if lt .UserID 0}} <span>UserID < 0</span> {{end}} {{if le .UserID 0}} <span>UserID <= 0</span> {{end}} {{if gt .UserID 0}} <span>UserID > 0</span> {{end}} {{if ge .UserID 0}} <span>UserID >= 0</span> {{end}} 

{{.Lang.pool} - Lang is a map. Those. through the point we get the value, which has the key “pool”

About Katya
About a week I did not write and did not call her. I decided to check if she would call first. It was kapets how difficult it was, suffered, but waited for her call, I wanted her to break the rules from the book.
In the end, gave up. I wrote to her, called to the cinema. She said that today can not, because is moving. From the help with the move refused.

I'll run a little ahead and say that after the same sms-ki "Do not call and do not write me more !!!!", I in the VC lured to my side her girlfriend, with whom she lived for 2 years, so that she helped me to return Kate . So, her girlfriend, said that the whole week, when I did not call, Kate was very worried and was waiting for a call from me. She also said that after the first date, Katya literally glowed with joy. She also added that she did not read any book on seduction.

User Functions


The html-code in the variables needs to be disassembled. To do this, add a function:

 funcMap := template.FuncMap{ "noescape": func(s string) template.HTML { return template.HTML(s) }, } 

All user functions are collected in my makeTemplate . Use this:

 {{noescape .Lang.htmlText}} 

About Katya
March 8th was near. Katya still did not want to meet with me. And I really wanted to congratulate her on the holiday. In the end, I put the question squarely. Either we meet on the 8th of March, or I will not call her again. We agreed to celebrate the holiday with me.

I bought her favorite flowers, wine, etc. I wanted to spend the whole day with her, but for dinner she was not ready yet, in an hour, too, and after two. To my surprise, by six o'clock she finally got ready. I called her a taxi.

Range


To disassemble any map used Range. Here is an example taken from here :

 {{range $k, $country := .Countries}} <option value='{{$k}}' {{if eq $k $.MyCountry}} selected {{end}}>{{$country}}</option> {{end}} 

From the code, probably, it is clear what is happening. But I want to pay special attention to $ .MyCountry. If you do not add $, then access to the .MyCountry parameter will not be. At first, I often forgot about it and thought for a long time what was wrong. Outside of range, access to MyCountry is again obtained through {{.MyCountry}}

About Katya
Arrived, handed flowers, gifts. Opened a bottle of wine. She saw my old PHP sources on one of the monitors. She asked - "What is it?". I told her about the blockchain, about how Dcoin, thanks to the natural desire of people to make a profit (“not getting up from the couch”), will be able to capture the minds of millions, smoothly replacing conventional currencies with decentralized ones, then the issue will stop and we will have a new world with transparent financial system. Katya did not understand anything. Then they watched a movie, kissed. Suddenly the wine ran out, I asked if she wanted more, she said she wanted. I went to the store.

Variables


In templates, you can create your own variables. Here is an example taken from here :

 {{range $i, $pageType := .PagesArray}} {{$counter := ""}} {{if eq $pageType "funders"}} {{$counter := " <span class='count h6 bg-grey-dark white'>{{$.ProjectCountFunders}}</span>"}} {{else if eq $pageType "comments"}} {{$counter := " <span class='count h6 bg-grey-dark white'>{{$.ProjectCountComments}}</span>"}} {{end}} {{$counter}} {{end}} 

In this example, a new $ counter variable is created, which is assigned a value, depending on the condition, and its result is displayed below.

and / or


The logic of work and / or turned out to be somewhat unusual for me.
Examples:

 {{if and (eq .Start 1) (ne .BlockId 1) (gt .UserId 0)}} 

If .Start == 1 and .BlockId! = 1 and .UserId> 0

 {{if or (eq .Start 1) (ne .BlockId 1) (gt .UserId 0)}} 

If .Start == 1 or .BlockId! = 1 or .UserId> 0

Js code


Another problem took me quite a lot of time. If you need to generate some js-code in the controller, for example:

 var x="1"; 

Then, when outputting it in the template via {{.SomeJSCode}} we get

 "var x=\"1\";" 

Solution 1


noescape will not help here. You need to add a new custom function:

 "js": func(s string) template.JS { return template.JS(s) }, 

And in the template use {{js .SomeJSCode}}

Solution 2


In the controller, use the template.JS type. Before converting the variable string: template.JS (SomeJsStringVariable). In the template itself, in this case you need to output {{.SomeJSCode}}

About Katya
Returned, finished the wine. Got massage oil. I had a massage for about an hour (the other day I specially studied this video here ). The massage smoothly translated into something more intimate, as a result of which she experienced a sharp release of endorphins, and immediately after that she said that she wanted to sleep. “What the fuck?” I thought. In general, we went to bed.
Continued in the next part.

Conclusion


In the following articles, I will talk about the database, smooth application termination via signals, blockchain block processing, GO encryption and JS decryption, how I changed gomobile a little, adding notifications and background work for IOS and Android applications.

Source: https://habr.com/ru/post/273333/


All Articles