Skip to content

4n4k1n/ternary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ternary

A simple Go package that provides a generic ternary operator function.

Installation

go get github.com/4n4k1n/ternary

Usage

import "github.com/4n4k1n/ternary"

// Basic usage
result := ternary.If(4 > 5, "yes", "no")  // returns "no"

// With numbers
max := ternary.If(a > b, a, b)  // Get maximum of two values

// With strings
status := ternary.If(isActive, "active", "inactive")

// Complex conditions
msg := ternary.If(age >= 18 && hasLicense, "can drive", "cannot drive")

Function Signature

func If[T any](condition bool, trueVal, falseVal T) T
  • condition: Any boolean expression
  • trueVal: Value to return if condition is true
  • falseVal: Value to return if condition is false
  • Returns: Either trueVal or falseVal depending on the condition

The function uses Go generics, so it works with any type as long as both return values are the same type.

Why Use This?

Go doesn't have a built-in ternary operator like condition ? trueVal : falseVal in other languages. This package provides a clean, reusable way to write simple conditional expressions without verbose if-else blocks.

License

MIT

About

Bring ternary operator syntax to Go with generics

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages