Skip to content

Instantly share code, notes, and snippets.

@zeroidentidad
Last active June 24, 2023 14:59
Show Gist options
  • Save zeroidentidad/5d0fdbd992b404abf017814962897891 to your computer and use it in GitHub Desktop.
Save zeroidentidad/5d0fdbd992b404abf017814962897891 to your computer and use it in GitHub Desktop.
Idea helper ternario en Go
package main
import (
"fmt"
)
func main() {
// Con tipos primitivos definidos
a := ternaryInt64(5 > 88, 13, 0)
b := ternaryString(3 > 1, "correcto", "incorrecto")
fmt.Println(a)
fmt.Println(b)
// Con interfaz vacia
c := Ternary(5 > 88, 13, 0).(int)
d := Ternary(3 > 1, "correcto", "incorrecto").(string)
fmt.Println(c)
fmt.Println(d)
}
// Con tipos primitivos definidos
func ternaryInt64(a bool, b, c int64) int64 {
if a {
return b
}
return c
}
func ternaryString(a bool, b, c string) string {
if a {
return b
}
return c
}
// Con interfaz vacia para extender con aserción de tipos
// Ternary da expresión ternaria simple pasando cualquier tipos de datos
func Ternary(a bool, b, c interface{}) interface{} {
if a {
return b
}
return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment