Skip to content

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))
}

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy