This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go func(ctx context.Context) { | |
for { | |
select { | |
case <-ctx.Done(): // Выход при отмене | |
return | |
case <-time.After(1 * time.Second): | |
fmt.Println("Working...") | |
} | |
} | |
}(ctx) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Плохо: горутина "утечёт", если контекст отменится. | |
go func() { | |
for { | |
select { | |
case <-time.After(1 * time.Second): | |
fmt.Println("Working...") | |
} | |
} | |
}() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type key string | |
const userKey key = "user" | |
ctx := context.WithValue(ctx, userKey, User{Name: "Alice"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Плохо: контекст — не место для сложных структур! | |
ctx := context.WithValue(context.Background(), "user", User{Name: "Alice"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ByteSize uint64 | |
const ( | |
_ = iota | |
KB ByteSize = 1 << (10 * iota) MB | |
GB | |
TB | |
PB | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
u = iota * 42 // iota == 0; u == 0 (нетипизированная целочисленная константа) | |
v float64 = iota * 42 // iota == 1; v == 42.0 (константа типа float64) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type EventType byte | |
const ( | |
_ = iota // iota == 0; игнорировать нулевое значение EventDelete | |
EventType = iota // iota == 1 | |
EventPut // iota == 2; неявное присваивание | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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] |
NewerOlder