Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/otiai10/ternary
Golang ternary expression :golf:
https://github.com/otiai10/ternary
go ternary
Last synced: 21 days 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 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-29T03:18:10.000Z (almost 7 years ago)
- Last Synced: 2024-10-05T06:16:23.757Z (about 1 month ago)
- Topics: go, ternary
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 14
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ternary
[![Build Status](https://travis-ci.org/otiai10/ternary.svg?branch=master)](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: !