Skip to content

Instantly share code, notes, and snippets.

View juev's full-sized avatar
🏠
Working from home

Evsyukov Denis juev

🏠
Working from home
View GitHub Profile

Лучшее

  • sourcegraph/conc – Better structured concurrency for go conc is your toolbelt for structured concurrency in go, making common tasks easier and safer.
  • ants – ants is a high-performance and low-cost goroutine pool in Go. Библиотека ants реализует пул горутин с фиксированной емкостью, управляющий и утилизирующий огромное количество горутин, что позволяет разработчикам ограничить количество горутин в ваших параллельных программах.
  • huandu/go-clone – Clone any Go data structure deeply and thoroughly. Package clone provides functions to deep clone any Go data. It also provides a wrapper to protect a pointer from any unexpected mutation. For users who use Go 1.18+, it's recommended to import github.com/huandu/go-clone/generic for generic APIs and arena support. Clone/Slowly can clone unexported fields and "no-copy" structs as well. Use this feature wisel
run:
modules-download-mode: readonly
skip-files:
- ".*\\.pb\\.go$"
linters:
fast: true
enable:
- errcheck
- godot
- gosimple

Go это не Java

источник

Make every struct field non-exported (starts with a lowercase letter in its name) and have nothing but exported functions that work with the hidden data. Make every function take an Interface and return nothing but struct that implements an exported Interface .

Сделайте каждое поле структуры неэкспортируемым (начинается со строчной буквы в его названии) и не имеющим ничего, кроме экспортированных функций, которые работают со скрытыми данными. Сделайте так, чтобы каждая функция принимала значение Interface и не возвращала ничего, кроме struct который реализует экспортированный Interface .

cmp.Or

источник

// Or returns the first of its arguments that is not equal to the zero value.
// If no argument is non-zero, it returns the zero value.
func Or[T comparable](vals ...T) T {
  var zero T

Decorator Pattern

Go program pattern 05 : Decorations

What is the Decorator Pattern?

The Decorator pattern is a design pattern that allows us to dynamically add behavior to an object at runtime without altering its implementation. This is achieved by creating a wrapper object or decorator that contains the original object and provides an enhanced interface to add new behavior.

In Go, we can use functions as decorators because Go supports higher-order functions, which means functions can be passed as parameters and returned as values.

Merge conflicts in go.sum

Источник

When rebasing a branch in a Go project, you may encounter git conflicts on your dependencies. While the go.mod file should still be handled manually, it seems safe to automatically merge the go.sum file by keeping both versions, and running a go mod tidy afterwards.

Git can handle the first part by itself:

 # if you don't already have a global attributes file:

Ergonomics

Источник

Use of a package like this may be pervasive if you really commit to it. This package was inspired by Rust's options implemenation. It might be worth considering dropping the repetative option. preceding the variants. Since importing names into the global namespace is to be avoided, the following import pattern may work for you:

import (
    "fmt"
 

Сборка образа golang приложений

Реализуется через обычный мультистейдж, образ с golang, в котором собирается приложение и затем готовый бинарник копируется в дистролесс образ.

Пример:

FROM golang AS build
WORKDIR /src
RUN GOBIN=/src go install github.com/nkanaev/yarr/cmd/yarr@latest

Tiniest Go docker image

источник

CGO_ENABLED=0 go build -ldflags="-s -w" app.go && tar C app | docker import - myimage:latest

From inside of a Docker container, how do I connect to the localhost of the machine?

Источник

If you are using Docker-for-mac or Docker-for-Windows 18.03+, connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).

If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option, or added the following snippet in your docker-compose.yml file:

extra_hosts: