conditional

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package conditional contains helper utilities such as a ternary operators and other functions for conditional types of operations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func If

func If[T any](cond bool, ifTrue, ifFalse T) T

If is a ternary function for returning a value if a condition is true and another if false.

Example
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	score := 85

	label := conditional.If(score >= 60, "pass", "fail")

	fmt.Println(label)
}
Output:
pass

func IfCall

func IfCall(cond bool, trueFunc, falseFunc func())

IfCall is a ternary function for calling a function if a condition is true and calling another if false.

Example
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	conditional.IfCall(
		true,
		func() { fmt.Println("welcome back") },
		func() { fmt.Println("please log in") },
	)
}
Output:
welcome back

func IfCallReturn

func IfCallReturn[T any](cond bool, trueFunc, falseFunc func() T) T

IfCallReturn is a ternary function for calling a function if a condition is true and calling another if false and returns the result.

Example
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	limit := conditional.IfCallReturn(
		false,
		func() int { return 100 },
		func() int { return 10 },
	)

	fmt.Println(limit)
}
Output:
10

func IfNotNil

func IfNotNil[T any](x *T, callee func())

IfNotNil calls a function if x is not nil.

Example
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	name := "Alice"

	conditional.IfNotNil(&name, func() {
		fmt.Println("name is set")
	})
}
Output:
name is set

func Switch added in v1.2.2

func Switch[K comparable, V any](key K, cases map[K]V, fallback V) V

Switch returns the value associated with key in the cases map. If the key is not present, fallback is returned. It is useful for concise value lookup that would otherwise require a switch statement or repeated if/else chains.

Example
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	status := 2

	label := conditional.Switch(status, map[int]string{
		1: "active",
		2: "inactive",
		3: "pending",
	}, "unknown")

	fmt.Println(label)
}
Output:
inactive
Example (Fallback)
package main

import (
	"fmt"

	"github.com/SharkByteSoftware/go-snk/conditional"
)

func main() {
	status := 99

	label := conditional.Switch(status, map[int]string{
		1: "active",
		2: "inactive",
		3: "pending",
	}, "unknown")

	fmt.Println(label)
}
Output:
unknown

Types

This section is empty.

Jump to

Keyboard shortcuts

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