gobuch/best-practices/context-value-nono/main.go

29 lines
432 B
Go
Raw Normal View History

2020-08-21 04:26:40 +00:00
package main
import (
"context"
"fmt"
)
type contextKey int
const usernameKey contextKey = iota
func businessCall(username string) {
//.. was wichtiges
fmt.Println(username)
}
func main() {
ctx := context.Background()
vCtx := context.WithValue(ctx, usernameKey, "bob")
//.. einiges anderes
businessCall(vCtx.Value(usernameKey).(string))
//Variante2
username := "bob"
//.. einiges anderes
businessCall(username)
}