Унмаршалинг или парсинг JSON на golang

package main

import (
	"encoding/json"
	"fmt"
)

func main() {

	type TmpData struct {
		Email string `json:"email"`
		Cart  []struct {
			Name  string `json:"name"`
			Price string `json:"price"`
			Count string `json:"count"`
			Total string `json:"total"`
		} `json:"cart"`
	}

	var tmpW TmpData

	empJson := `{"email":"asdf@adsf.com","cart":[{"name":"Товар 1","price":"79.99","count":1,"total":"79.99"},{"name":"Товар 2","price":"179.99","count":1,"total":"79.99"}]}`

	json.Unmarshal([]byte(empJson), &tmpW)

	fmt.Printf("%+v\n", tmpW.Cart[1].Name)

	for _, v := range tmpW.Cart {
		fmt.Println("value:", v.Name)
	}

}

Валидация электронной почты с проверкой MX на Golang

package main

import (
	"net"
	"regexp"
	"strings"
)

func isEmailValid(e string) bool {

	var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")

	if len(e) < 3 || len(e) > 254 {
		return false
	}
	if !emailRegex.MatchString(e) {
		return false
	}
	parts := strings.Split(e, "@")
	mx, err := net.LookupMX(parts[1])
	if err != nil || len(mx) == 0 {
		return false
	}
	return true

}

Отправка почты Sendmail SMTP яндекс на Golang

package main

import (
	"crypto/tls"
	"fmt"
	"log"
	"net"
	"net/mail"
	"net/smtp"
)

func sendmail(toEmail string, subj string, body string) {

	from := mail.Address{Name: "Name", Address: "no-replay@mail.com"}
	to := mail.Address{Name: "", Address: toEmail}

	headers := make(map[string]string)
	headers["From"] = from.String()
	headers["To"] = to.String()
	headers["Subject"] = subj

	message := ""
	for k, v := range headers {
		message += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	message += "\r\n" + body

	servername := "smtp.yandex.ru:465"

	host, _, _ := net.SplitHostPort(servername)

	auth := smtp.PlainAuth("", "LOGIN", "PASSWORD", host)

	tlsconfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         host,
	}

	conn, err := tls.Dial("tcp", servername, tlsconfig)
	if err != nil {
		log.Panic(err)
	}

	a, err := smtp.NewClient(conn, host)
	if err != nil {
		log.Panic(err)
	}

	// Auth
	if err = a.Auth(auth); err != nil {
		log.Panic(err)
	}

	// To && From
	if err = a.Mail(from.Address); err != nil {
		log.Panic(err)
	}

	if err = a.Rcpt(to.Address); err != nil {
		log.Panic(err)
	}

	// Data
	w, err := a.Data()
	if err != nil {
		log.Panic(err)
	}

	_, err = w.Write([]byte(message))
	if err != nil {
		log.Panic(err)
	}

	err = w.Close()
	if err != nil {
		log.Panic(err)
	}

	a.Quit()

}

RoadRunner – высокопроизводительный сервер приложений PHP, балансировщик нагрузки и менеджер процессов, написанный на Golang

<?php
// worker.php
ini_set('display_errors', 'stderr');
include "vendor/autoload.php";

$relay = new Spiral\Goridge\StreamRelay(STDIN, STDOUT);
$psr7 = new Spiral\RoadRunner\PSR7Client(new Spiral\RoadRunner\Worker($relay));

while ($req = $psr7->acceptRequest()) {
    try {
        $resp = new \Zend\Diactoros\Response();
        $resp->getBody()->write("hello world");

        $psr7->respond($resp);
    } catch (\Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

https://github.com/spiral/roadrunner

Scroll Up