Skip to content

Instantly share code, notes, and snippets.

View degibenz's full-sized avatar

Degibenz degibenz

View GitHub Profile
@degibenz
degibenz / example.go
Created April 10, 2025 06:12
example.go
func main() {
server := &http.Server{Addr: ":8080"}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@degibenz
degibenz / example.go
Created April 10, 2025 06:12
example.go
go func(ctx context.Context) {
for {
select {
case <-ctx.Done(): // Выход при отмене
return
case <-time.After(1 * time.Second):
fmt.Println("Working...")
}
}
}(ctx)
@degibenz
degibenz / example.go
Created April 10, 2025 06:11
example.go
// Плохо: горутина "утечёт", если контекст отменится.
go func() {
for {
select {
case <-time.After(1 * time.Second):
fmt.Println("Working...")
}
}
}()
@degibenz
degibenz / example.go
Created April 10, 2025 06:10
example.go
type key string
const userKey key = "user"
ctx := context.WithValue(ctx, userKey, User{Name: "Alice"})
@degibenz
degibenz / example.go
Created April 10, 2025 06:10
example.go
// Плохо: контекст — не место для сложных структур!
ctx := context.WithValue(context.Background(), "user", User{Name: "Alice"})
@degibenz
degibenz / example.go
Created April 10, 2025 06:08
example.go
func handler(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel() // Освобождаем ресурсы
result, err := someLongOperation(ctx)
if err != nil {
http.Error(w, "Operation timed out", http.StatusGatewayTimeout)
return
}
fmt.Fprintf(w, "Result: %v", result)
type ByteSize uint64
const (
_ = iota
KB ByteSize = 1 << (10 * iota) MB
GB
TB
PB
)
const (
u = iota * 42 // iota == 0; u == 0 (нетипизированная целочисленная константа)
v float64 = iota * 42 // iota == 1; v == 42.0 (константа типа float64)
)
type EventType byte
const (
_ = iota // iota == 0; игнорировать нулевое значение EventDelete
EventType = iota // iota == 1
EventPut // iota == 2; неявное присваивание
)
[266] → terraform plan -out myplan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
data.consul_keys.sub_network_id: Refreshing state...
data.consul_keys.network_id: Refreshing state...
openstack_blockstorage_volume_v1.volume[0]: Refreshing state... [id=4a90df7c-9d4b-4488-a958-72ef0d261b0e]
random_pet.server: Refreshing state... [id=knowing-pig]
openstack_compute_instance_v2.server[0]: Refreshing state... [id=75f5dc9a-4079-4f44-a3bf-5be7bb66e723]