https://github.com/otiai10/ternary
Golang ternary expression :golf:
https://github.com/otiai10/ternary
go ternary
Last synced: about 1 year ago
JSON representation
Golang ternary expression :golf:
- Host: GitHub
- URL: https://github.com/otiai10/ternary
- Owner: otiai10
- Created: 2015-03-09T03:22:52.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-11-29T03:18:10.000Z (over 8 years ago)
- Last Synced: 2025-04-15T14:45:34.247Z (about 1 year ago)
- Topics: go, ternary
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ternary
[](https://travis-ci.org/otiai10/ternary)
Ternary expression for golang, to enjoy code-golf :golf:
```go
port := ternary.String(os.Getenv("PORT"))("8080")
// Returns "8080" if "PORT" == ""
```
# why
If you want
```go
status := 500
if flag {
status = 200
}
```
Write
```go
status := ternary.If(flag).Int(200, 500)
```
If you want
```go
res := map[string]interface{}{
"user": user,
"expired_at": nil,
}
if user.Expire() {
res["expire_at"] = time.Now()
}
json.NewEncoder(wr).Encode(res)
```
Write
```go
json.NewEncoder(wr).Encode(map[string]interface{}{
"user": user,
"expired_at": ternary.If(user.Expire()).Interface(time.Now(), nil),
})
```
Enjoy :golf: !