{"id":20741103,"url":"https://github.com/robtimus/go-ternary","last_synced_at":"2026-04-21T00:31:24.057Z","repository":{"id":255354939,"uuid":"849319733","full_name":"robtimus/go-ternary","owner":"robtimus","description":"Provides a simple implementation of ternary expressions","archived":false,"fork":false,"pushed_at":"2025-02-28T20:06:37.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T22:59:20.842Z","etag":null,"topics":["golang","ternary","ternary-expression"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robtimus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-29T11:41:31.000Z","updated_at":"2025-02-28T20:06:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"4b2845e8-6f0c-464a-8bdb-d8d5f39b7b2e","html_url":"https://github.com/robtimus/go-ternary","commit_stats":null,"previous_names":["robtimus/go-ternary"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robtimus%2Fgo-ternary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robtimus%2Fgo-ternary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robtimus%2Fgo-ternary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robtimus%2Fgo-ternary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robtimus","download_url":"https://codeload.github.com/robtimus/go-ternary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243030805,"owners_count":20224666,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","ternary","ternary-expression"],"created_at":"2024-11-17T06:33:48.269Z","updated_at":"2026-04-21T00:31:24.044Z","avatar_url":"https://github.com/robtimus.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-ternary\n[![Build Status](https://github.com/robtimus/go-ternary/actions/workflows/build.yml/badge.svg)](https://github.com/robtimus/go-ternary/actions/workflows/build.yml)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=robtimus%3Ago-ternary\u0026metric=alert_status)](https://sonarcloud.io/summary/overall?id=robtimus%3Ago-ternary)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=robtimus%3Ago-ternary\u0026metric=coverage)](https://sonarcloud.io/summary/overall?id=robtimus%3Ago-ternary)\n\nA simple implementation of ternary expressions in Go.\n\nThe Go language does not support ternary expressions. That means that you have to write quite some boilerplate code to achieve the same:\n\n```go\nvar result TYPE\nif condition {\n    result = trueResult\n} else {\n    result = falseResult\n}\n```\n\nThis module allows you to do the same with just a single line. To allow Go to infer the generic type, ternary expressions need to be written as in Python: `trueResult if condition else falseResult`:\n\n```go\nresult := ternary.Return(trueResult).When(condition).Else(falseResult)\n```\n\n## Lazy evaluation\n\nThe `Return` and `Else` above both require the values to be evaluated eagerly. For constants, pre-existing variables and simple expressions this is fine. However, for more complex expressions it makes more sense to use lazy evaluation. That can be achieved using `Call` and `ElseCall`:\n\n```go\nresult := ternary.Call(func() TYPE { ... }).When(condition).ElseCall(func() TYPE { ... })\n```\n\nIt's of course also possible to mix eager and lazy evaluation:\n\n```go\nresult1 := ternary.Return(trueResult).When(condition).ElseCall(func() TYPE { ... })\nresult2 := ternary.Call(func() TYPE { ... }).When(condition).Else(falseResult)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobtimus%2Fgo-ternary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobtimus%2Fgo-ternary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobtimus%2Fgo-ternary/lists"}