Skip to content

Package conditional is go/golang replacement for ternary if/else operator

License

Notifications You must be signed in to change notification settings

mileusna/conditional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go/Golang replacement for ternary if/else operator GoDoc

Package conditinal is go/golang replacement for ternary if/else operator

Many languages have ternary operator which makes it easy and short to assign value inline, for example

// pseudocode, not the go code :(
val := (x==y) ? "Value OK" : "Value not OK" 

Unfortunately, there is no ternary testing operator in Go so the shortest way to write this code in go would be

val := "Value not OK"
if x==y {
    val = "Value OK"
}

or if you prefere perhapse more readable but longer version

var val string
if x==y {
    val = "Value OK"
} else {
    val = "Value not OK"
}

This is where conditional package steps in. It provides fuctions that replaces ternary operator for each basic type in go. We can now write conditional assignment in only one Go line:

val := conditional.String(x==y, "Value OK", "Value not OK")

Package conditional also provides fuctions for all Go basic types

n := conditional.Int(x==y, 20, 0)
u := conditional.UInt(true, 23, 15)
f := conditional.Float64(true, 23.2, 15.1)
// etc. etc.

// even for interface{}
i := conditional.Interface(x==y, "Great", 10)

Example:

package main

import (
    "fmt"
    "github.com/mileusna/conditional"
)

func main() {
    x := 2
    y := 3
    fmt.Println(conditional.Int(x==y, 20, 0))
    fmt.Println(conditional.String(x==y, "Value OK", "Value not OK"))
    fmt.Println(conditional.UInt(true, 23, 15))
    fmt.Println(conditional.Float64(true, 23.4, 15.1))
}