File: example_test.go

package info (click to toggle)
golang-github-icza-gox 0.0~git20210726.cd40a3f-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 332 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package gox_test

import (
	"fmt"
	"strconv"
	"time"

	. "github.com/icza/gox/gox"
)

func Example() {
	// Pointers to non-zero values
	b, i, s := NewBool(true), NewUint(1), NewString("hi")
	fmt.Printf("b: %t, i: %d, s: %s\n", *b, *i, *s)

	// One way of mimicing the ternary operator:
	for _, age := range []int{10, 20} {
		state := If(age < 18).String("child", "adult")
		fmt.Printf("Age: %d, state: %s\n", age, state)
	}

	// And another one:
	for _, tempC := range []int{-5, 10} {
		fmt.Printf("Temperature: %d°C, state: %s\n",
			tempC, IfString(tempC < 0, "solid", "liquid"))
	}

	// Pass multiple return values to variadic functions:
	now := time.Date(2020, 3, 4, 0, 0, 0, 0, time.UTC)
	fmt.Printf("Year: %d, month: %d, day: %d\n",
		Wrap(now.Date())...)

	// Quick "handling" of error:
	n, err := strconv.Atoi("3")
	Pie(err)
	fmt.Println("Parsed:", n)

	// Output:
	// b: true, i: 1, s: hi
	// Age: 10, state: child
	// Age: 20, state: adult
	// Temperature: -5°C, state: solid
	// Temperature: 10°C, state: liquid
	// Year: 2020, month: 3, day: 4
	// Parsed: 3
}