operator

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ternary

func Ternary[T any](condition bool, a, b T) (res T)

Ternary returns the first value if the condition is true, and returns the second value if the the condition is false

Example
package main

import (
	"fmt"

	"github.com/alpardfm/go-toolkit/operator"
)

func main() {
	// Generic ternary operator
	result := operator.Ternary(true, "yes", "no")
	fmt.Println(result)

	result2 := operator.Ternary(false, "yes", "no")
	fmt.Println(result2)

	// Works with any type
	num := operator.Ternary(10 > 5, 100, 0)
	fmt.Println(num)

}
Output:
yes
no
100

func TernaryFloat

func TernaryFloat(condition bool, a, b float64) (res float64)

TernaryFloat returns the first value if the condition is true, and returns the second value if the condition is false

Example
package main

import (
	"fmt"

	"github.com/alpardfm/go-toolkit/operator"
)

func main() {
	result := operator.TernaryFloat(true, 1.5, 2.5)
	fmt.Printf("%.1f\n", result)

	result2 := operator.TernaryFloat(false, 1.5, 2.5)
	fmt.Printf("%.1f\n", result2)

}
Output:
1.5
2.5

func TernaryString

func TernaryString(condition bool, a, b string) (str string)

TernaryString returns the first value if the condition is true, and returns the second value if the condition is false

Example
package main

import (
	"fmt"

	"github.com/alpardfm/go-toolkit/operator"
)

func main() {
	result := operator.TernaryString(true, "active", "inactive")
	fmt.Println(result)

	result2 := operator.TernaryString(false, "active", "inactive")
	fmt.Println(result2)

}
Output:
active
inactive

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL