Работа со строками в golang



package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(    
        // true
        strings.Contains("test", "es"), 

        // 2
        strings.Count("test", "t"),

        // true
        strings.HasPrefix("test", "te"), 

        // true
        strings.HasSuffix("test", "st"), 

        // 1
        strings.Index("test", "e"), 

        // "a-b"
        strings.Join([]string{"a","b"}, "-"),

        // == "aaaaa"
        strings.Repeat("a", 5), 

        // "bbaa"
        strings.Replace("aaaa", "a", "b", 2),

        // []string{"a","b","c","d","e"}
        strings.Split("a-b-c-d-e", "-"), 

        // "test"
        strings.ToLower("TEST"), 

        // "TEST"
        strings.ToUpper("test"), 

    )
}

Golang interface

Проверка, что наша структура user реализует интерфейс User
Создаем переменную которая не нужна с типом User и значением структуры user



var _ User = &user{}

type User interface {
	ChangFIO(newFio string)
}

type user struct {
	FIO string
}

func (u *user) ChangFIO(newFio string) {

}


Invoke golang



package main

import (
	"fmt"
	"reflect"
)

var funcMap = map[string]interface{}{
	"Hello": Hello,
}

func Hello(name string) {
	fmt.Printf("Hello, %s!\n", name)
}

func main() {
	nameFunc := "Hello"

	if fn, ok := funcMap[nameFunc]; ok {
		funcValue := reflect.ValueOf(fn)
		args := []reflect.Value{reflect.ValueOf("Gopher")}
		funcValue.Call(args)
	} else {
		fmt.Println("Function not found!")
	}
}


gracefully shutdown HTTP server

	// listen to OS signals and gracefully shutdown HTTP server
	stopped := make(chan struct{})
	go func() {
		sigint := make(chan os.Signal, 1)
		signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
		<-sigint
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		if err := srv.Shutdown(ctx); err != nil {
			log.Printf("HTTP Server Shutdown Error: %v", err)
		}
		close(stopped)
	}()

	log.Printf("Starting HTTP server on %s", cfg.HTTPAddr)

	// start HTTP server
	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
		log.Fatalf("HTTP server ListenAndServe Error: %v", err)
	}

	<-stopped

Conventional Commits

https://www.conventionalcommits.org/en/v1.0.0/#summary

 

 

The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages