simple tern expression condition ? expression 1 : expression 2
in golang
Lightweight and adaptable Go package for streamlined condition-based logic with elegant tern expressions.
π― Generic Tern Operations: Type-safe condition-based expressions with comprehensive generic support
β‘ Deferred Evaluation: Deferred execution with function-based parameters to optimize performance
π Adaptable Conditions: Support for both boolean and function-based condition-based evaluation
π Zero-Value Handling: Auto zero-value fallbacks and dedicated zero-value comparison utilities
π Multiple Evaluation Modes: Direct values, functions, and zero-value aware operations
go install github.com/yyle88/tern@latest
This example demonstrates the core tern expression features with different evaluation modes.
package main
import (
"fmt"
"github.com/yyle88/tern"
)
func main() {
// Basic condition-based selection
result := tern.BVV(true, "Option A", "Option B")
fmt.Println(result) // Output: Option A
// Deferred execution for fallback value
result = tern.BVF(false, "Default", func() string { return "Computed Fallback" })
fmt.Println(result) // Output: Computed Fallback
// Handling zero values as fallback
result = tern.BV(false, "Fallback")
fmt.Println(result) // Output: (blank string)
}
β¬οΈ Source: Source
The zerotern
subpackage provides utilities for zero-value aware condition-based logic.
package main
import (
"fmt"
"github.com/yyle88/tern/zerotern"
)
func main() {
// Direct zero-value comparison
result := zerotern.VV("non-zero", "fallback")
fmt.Println(result) // Output: non-zero
// Function-based fallback for zero values
result = zerotern.VF("", func() string { return "fallback func" })
fmt.Println(result) // Output: fallback func
}
β¬οΈ Source: Source
The tern
package provides comprehensive condition-based evaluation functions:
Function | Condition Type | Main Value | Fallback Value |
---|---|---|---|
BVV |
bool |
Direct value | Direct value |
BVF |
bool |
Direct value | Function returning value |
BFV |
bool |
Function returning value | Direct value |
BFF |
bool |
Function returning value | Function returning value |
FVV |
func() bool |
Direct value | Direct value |
FVF |
func() bool |
Direct value | Function returning value |
FFV |
func() bool |
Function returning value | Direct value |
FFF |
func() bool |
Function returning value | Function returning value |
Function | Condition Type | Main Value | Fallback Value |
---|---|---|---|
BV |
bool |
Direct value | Zero value of type T |
BF |
bool |
Function returning value | Zero value of type T |
FV |
func() bool |
Direct value | Zero value of type T |
FF |
func() bool |
Function returning value | Zero value of type T |
Performance optimization through deferred execution:
func expensiveComputation() string {
fmt.Println("Computing...")
return "Complex Result"
}
result := tern.BVF(false, "Default", expensiveComputation)
// Output: Default (expensiveComputation is not executed)
Get zero value for all generic types:
fmt.Println(tern.Zero[int]()) // Output: 0
fmt.Println(tern.Zero[string]()) // Output: (blank string)
The zerotern
subpackage provides dedicated utilities for zero-value aware operations:
Function | Comparison Type | Main Value | Fallback Value |
---|---|---|---|
VV |
Direct comparison | Direct value | Direct value |
VF |
Direct comparison | Direct value | Function returning value |
FV |
Function comparison | Function returning value | Direct value |
FF |
Function comparison | Function returning value | Function returning value |
Function | Ref Handling | Fallback Value |
---|---|---|
SetPV |
Ref to direct value | Direct value |
SetPF |
Ref to direct value | Function returning value |
Set ref values with zero-value awareness:
package main
import (
"fmt"
"github.com/yyle88/tern/zerotern"
)
func main() {
var value int
zerotern.SetPV(&value, 42)
fmt.Println(value) // Output: 42
value = 7
zerotern.SetPF(&value, func() int { return 99 })
fmt.Println(value) // Output: 7 (not changed since now non-zero)
}
β¬οΈ Source: Source
- Enhanced Code clearness: Simplifies condition-based logic with concise expressions
- Performance Optimization: Deferred evaluation avoids needless computations
- Type Protection: Leverages Go's generics for maximum adaptiveness and robustness
- Comprehensive Coverage: Supports various condition-based scenarios including zero-value handling
- Simple Integration: Drop-in replacement for complex if-else chains
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a bug? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate: Navigate to the cloned project (
cd repo-name
) - Branch: Create a feature branch (
git checkout -b feature/xxx
). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...
) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .
) - Commit: Commit changes (
git commit -m "Add feature xxx"
) ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx
). - PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! π