op

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 0 Imported by: 1

README

Operator

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func If added in v2.3.0

func If[T any](cond bool, trueVal, falseVal T) T

If 倚靠generic讓回傳的型別可與參數相同

Example
i := 30
fmt.Println(If(i >= 60, "OK", "Fail"))
i = 60
fmt.Println(If(i >= 60, 600, 0))

// 如果If沒有使用generic,這種用法會報錯: invalid operation: cannot call non-function msgFunc (variable of type any)
msgFunc := If(i >= 60, func() {
	fmt.Println("123")
}, func() {
	fmt.Println("Fail")
})
msgFunc()
Output:

Fail
600
123

func Ternary deprecated

func Ternary(statement bool, a, b any) any

Deprecated: Use If instead of it. 這種用法回傳的型別就只能any,還要透過斷言才有辦法變成所想要的型別

Example

範例說明: Ternary

i := 30
fmt.Println(Ternary(i%2 == 0, "even", "odd").(string))
fmt.Println(Ternary(i > 30, 300, -1).(int))
Output:

even
-1

Types

This section is empty.

Jump to

Keyboard shortcuts

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