Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

tern is a lightweight Go package for simple, concise ternary expressions, enabling clear conditional logic.

License

NotificationsYou must be signed in to change notification settings

yyle88/tern

Repository files navigation

GitHub Workflow Status (branch)GoDocCoverage StatusSupported Go VersionsGitHub ReleaseGo Report Card

tern

tern is a lightweight and versatile Go package designed to streamline conditional logic with concise ternary expressions, helping you write clean, expressive, and maintainable code with ease.

README

中文说明

Features

  • Generic Support: Fully leverages Go’s generics for type safety and flexibility across various data types.
  • Flexible Logic: Provides robust support for both boolean conditions and conditional functions.
  • Lazy Evaluation: Optimizes performance by computing values only when necessary through deferred execution.
  • Zero Value Handling: Offers utilities to return default zero values for any type when no fallback is supplied.

Installation

go get github.com/yyle88/tern

Usage

Thetern package provides multiple helper functions tailored to handle various conditional scenarios.

Basic Usage

package mainimport ("fmt""github.com/yyle88/tern")funcmain() {// Basic conditional selectionresult:=tern.BVV(true,"Option A","Option B")fmt.Println(result)// Output: Option A// Deferred execution for fallback valueresult=tern.BVF(false,"Default",func()string {return"Computed Fallback" })fmt.Println(result)// Output: Computed Fallback// Handling zero values as fallbackresult=tern.BV(false,"Fallback")fmt.Println(result)// Output: (empty string)}

Function Overview

Here is an overview of the functions provided by thetern package:

FunctionCondition TypePrimary ValueFallback Value
BVVboolDirect valueDirect value
BVFboolDirect valueFunction returning value
BFVboolFunction returning valueDirect value
BFFboolFunction returning valueFunction returning value
FVVfunc() boolDirect valueDirect value
FVFfunc() boolDirect valueFunction returning value
FFVfunc() boolFunction returning valueDirect value
FFFfunc() boolFunction returning valueFunction returning value

Lazy Evaluation Example

Deferred execution ensures unnecessary computations are avoided, making your code more efficient:

funcexpensiveComputation()string {fmt.Println("Computing...")return"Heavy Result"}result:=tern.BVF(false,"Default",expensiveComputation)// Output: Default (expensiveComputation is not executed)

Working with Zero Values

The package providesZero[T](), a utility that returns the zero value for any generic type:

package mainimport ("fmt""github.com/yyle88/tern")funcmain() {fmt.Println(tern.Zero[int]())// Output: 0fmt.Println(tern.Zero[string]())// Output: (empty string)}

Handling Zero-Value Fallbacks

The package includes functions that automatically handle zero values when the condition is not met:

FunctionCondition TypePrimary ValueFallback Value
BVboolDirect valueZero value of typeT
BFboolFunction returning valueZero value of typeT
FVfunc() boolDirect valueZero value of typeT
FFfunc() boolFunction returning valueZero value of typeT

Additional Utilities in thezerotern Package

Thezerotern subpackage extendstern with specialized utilities for comparing values to their zero value, adding more control for fallback scenarios.

FunctionComparison TypePrimary ValueFallback Value
VVDirect comparisonDirect valueDirect value
VFDirect comparisonDirect valueFunction returning value

Example: UsingVV andVF

package mainimport ("fmt""github.com/yyle88/tern/zerotern")funcmain() {// Direct comparisonresult:=zerotern.VV("non-zero","fallback")fmt.Println(result)// Output: non-zero// Fallback with functionresult=zerotern.VF("",func()string {return"fallback func" })fmt.Println(result)// Output: fallback func}

Pointer-Based Utility Functions inzerotern

FunctionPointer HandlingFallback Value
SetPVPointer to direct valueDirect value
SetPFPointer to direct valueFunction returning value

Example: UsingSetPV andSetPF

package mainimport ("fmt""github.com/yyle88/tern/zerotern")funcmain() {varvalueintzerotern.SetPV(&value,42)fmt.Println(value)// Output: 42value=7zerotern.SetPF(&value,func()int {return99 })fmt.Println(value)// Output: 7}

Why Choosetern?

  1. Improved Readability: Simplifies conditional logic with concise and clear expressions.
  2. Performance Optimization: Lazy evaluation avoids unnecessary computations.
  3. Type Safety: Leverages Go’s generics for maximum flexibility and reliability.
  4. Versatility: Supports a wide range of scenarios, including pointer handling and zero-value fallbacks.

Contributing

Contributions are welcome! Feel free to report issues, suggest improvements, or submit pull requests onGitHub.

License

This project is licensed under the MIT License. See theLICENSE file for details.


Contributing

Feel free to contribute or improve the package! Stars and pull requests are always welcome!

Thank you for usingtern!


Starring

starring

Give me stars! Thank you!!!



[8]ページ先頭

©2009-2025 Movatter.jp