ternary

Golang ternary expression

Stars
14
Committers
1

ternary

Ternary expression for golang, to enjoy code-golf ⛳

port := ternary.String(os.Getenv("PORT"))("8080")
// Returns "8080" if "PORT" == ""

why

If you want

status := 500
if flag {
    status = 200
}

Write

status := ternary.If(flag).Int(200, 500)

If you want

res := map[string]interface{}{
    "user":       user,
    "expired_at": nil,
}
if user.Expire() {
    res["expire_at"] = time.Now()
}
json.NewEncoder(wr).Encode(res)

Write

json.NewEncoder(wr).Encode(map[string]interface{}{
    "user":       user,
    "expired_at": ternary.If(user.Expire()).Interface(time.Now(), nil),
})

Enjoy ⛳ !